📖 Scenario: Imagine you work at a company that collects customer feedback through online reviews. These reviews are written in plain text. To understand what customers like or dislike, you need to process this text data.
🎯 Goal: You will create a simple R program that stores some customer reviews, sets a keyword to search for, counts how many reviews contain that keyword, and then prints the count. This shows why text processing is common: to find useful information in written data.
📋 What You'll Learn
Create a vector called reviews with exactly these three strings: "Great product, fast delivery", "Poor packaging, but good quality", "Excellent customer service"
Create a variable called keyword and set it to the string "good"
Use a for loop with the variable review to go through each element in reviews
Inside the loop, check if keyword is found in review using grepl()
Create a variable called count to keep track of how many reviews contain the keyword
Print the final value of count
💡 Why This Matters
🌍 Real World
Companies often collect customer feedback, social media posts, or product reviews as text. Processing this text helps find common opinions or problems quickly.
💼 Career
Text processing skills are useful for jobs in data analysis, marketing, customer support, and anywhere understanding written feedback or documents is important.
Progress0 / 4 steps
1
Create the reviews vector
Create a vector called reviews with exactly these three strings: "Great product, fast delivery", "Poor packaging, but good quality", and "Excellent customer service"
R Programming
Hint
Use the c() function to create a vector with the three exact strings.
2
Set the keyword variable
Create a variable called keyword and set it to the string "good"
R Programming
Hint
Assign the string "good" to the variable keyword.
3
Count reviews containing the keyword
Create a variable called count and set it to 0. Then use a for loop with the variable review to go through each element in reviews. Inside the loop, use grepl(keyword, review) to check if keyword is in review. If yes, add 1 to count.
R Programming
Hint
Start count at 0. Use a for loop to check each review. Use grepl() to find the keyword.
4
Print the count
Write a print() statement to display the value of count
R Programming
Hint
Use print(count) to show how many reviews contain the keyword.
Practice
(1/5)
1. Why is text processing commonly used in programming with R?
easy
A. To clean and analyze words and sentences in data
B. To create graphical plots and charts
C. To perform complex mathematical calculations
D. To manage database connections
Solution
Step 1: Understand the purpose of text processing
Text processing helps handle words and sentences, which are common in data analysis.
Step 2: Identify the main use in R
R uses text processing mainly to clean and analyze textual data, not for graphics or math.
Final Answer:
To clean and analyze words and sentences in data -> Option A
Quick Check:
Text processing = cleaning and analyzing text [OK]
Hint: Text processing = working with words and sentences [OK]
Common Mistakes:
Confusing text processing with plotting
Thinking text processing is for math calculations
Mixing text processing with database management
2. Which of the following is the correct way to convert a string to lowercase in R?
easy
A. strtolower("HELLO")
B. lowercase("HELLO")
C. to_lower("HELLO")
D. tolower("HELLO")
Solution
Step 1: Recall R's built-in function for lowercase conversion
R uses the function tolower() to convert strings to lowercase.
Step 2: Check the options for correct syntax
Only tolower("HELLO") matches R's syntax; others are invalid or from other languages.
Final Answer:
tolower("HELLO") -> Option D
Quick Check:
tolower() converts text to lowercase [OK]
Hint: Use tolower() to convert text to lowercase in R [OK]
Common Mistakes:
Using functions from other languages like strtolower
Assuming lowercase() or to_lower() exist in R
Missing parentheses or quotes in function call
3. What will be the output of this R code?
text <- "Data Science"
words <- strsplit(text, " ")[[1]]
length(words)
medium
A. 1
B. 2
C. 11
D. Error
Solution
Step 1: Understand strsplit function
strsplit splits the string "Data Science" by space " ", resulting in a list with two words: "Data" and "Science".
Step 2: Extract the first element and count words
Using [[1]] extracts the vector of words, and length() counts how many words are in it, which is 2.
Final Answer:
2 -> Option B
Quick Check:
strsplit splits text into words = 2 [OK]
Hint: strsplit splits text into words; length counts them [OK]
Common Mistakes:
Counting characters instead of words
Forgetting to extract list element with [[1]]
Expecting an error due to syntax
4. Identify the error in this R code for replacing 'cat' with 'dog' in a string:
text <- "The cat sat on the mat"
new_text <- replace(text, "cat", "dog")
new_text
medium
A. replace() is not the correct function for string substitution
B. Missing assignment operator <-
C. Incorrect string quotes used
D. No error, code runs fine
Solution
Step 1: Check the function used for string replacement
In R, replace() is for replacing elements in vectors by index, not for string substitution.
Step 2: Identify correct function for string substitution
To replace text inside strings, sub() or gsub() should be used instead of replace().
Final Answer:
replace() is not the correct function for string substitution -> Option A
Quick Check:
Use sub() or gsub() for string replacement [OK]
Hint: Use sub() or gsub() for replacing text, not replace() [OK]
Common Mistakes:
Using replace() for string substitution
Assuming replace() works like sub()
Ignoring function purpose differences
5. You have a vector of sentences:
sentences <- c("R is great", "I love coding", "Data is powerful")
Which R code correctly creates a vector of the first words from each sentence?
hard
A. substr(sentences, 1, 1)
B. strsplit(sentences, " ")[1]
C. sapply(strsplit(sentences, " "), `[`, 1)
D. tolower(sentences)
Solution
Step 1: Understand the goal
We want the first word from each sentence in the vector.
Step 2: Analyze each option
substr(sentences, 1, 1) extracts only the first character, not the first word. strsplit(sentences, " ")[1] returns only the first element of the list, not all first words. sapply(strsplit(sentences, " "), `[`, 1) uses strsplit to split each sentence by space, then sapply extracts the first word from each split vector correctly. tolower(sentences) converts all text to lowercase, unrelated to extracting words.
Final Answer:
sapply(strsplit(sentences, " "), `[`, 1) -> Option C
Quick Check:
Use sapply + strsplit + `[`, 1 to get first words [OK]
Hint: Use sapply with strsplit and `[`, 1 to get first words [OK]
Common Mistakes:
Using substr() to get words instead of characters
Taking only first list element instead of all
Confusing lowercase conversion with word extraction
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.