0
0
R Programmingprogramming~15 mins

grep and grepl in R Programming - Mini Project: Build & Apply

Choose your learning style9 modes available
Using grep and grepl in R to Find Words in a List
📖 Scenario: Imagine you have a list of fruit names and you want to find which fruits contain the word "apple".
🎯 Goal: You will learn how to use grep and grepl functions in R to search for words inside a list of strings.
📋 What You'll Learn
Create a character vector with fruit names
Create a search word variable
Use grep to find positions of fruits containing the search word
Use grepl to get a logical vector showing which fruits contain the search word
Print the results
💡 Why This Matters
🌍 Real World
Searching text data for specific words or patterns is common in data analysis, such as finding keywords in customer feedback or filtering product names.
💼 Career
Knowing how to use <code>grep</code> and <code>grepl</code> helps in data cleaning, text mining, and preparing data for reports or machine learning.
Progress0 / 4 steps
1
Create a vector of fruit names
Create a character vector called fruits with these exact values: "apple", "banana", "pineapple", "orange", "grape"
R Programming
Need a hint?

Use the c() function to combine the fruit names into a vector.

2
Create a search word variable
Create a variable called search_word and set it to the string "apple"
R Programming
Need a hint?

Just assign the string "apple" to the variable search_word.

3
Use grep and grepl to find fruits containing the search word
Use grep with search_word and fruits to find the positions of fruits containing "apple" and store it in positions. Then use grepl with the same inputs to get a logical vector and store it in matches.
R Programming
Need a hint?

Remember grep returns positions (numbers), and grepl returns TRUE or FALSE for each element.

4
Print the results
Print the positions vector and then print the matches vector to show which fruits contain the word "apple".
R Programming
Need a hint?

Use print() to show the vectors.