Text processing is a key part of many programs. Why is it so common?
Think about what kinds of information people share and store every day.
Text is everywhere: in messages, files, websites, and logs. Programs often need to read, analyze, or change this text, making text processing very common.
Look at this R code that counts words in a sentence. What will it print?
sentence <- "R is great for text processing" words <- strsplit(sentence, " ")[[1]] length(words)
Count how many words are in the sentence after splitting by spaces.
The sentence has 6 words separated by spaces. The strsplit function splits the sentence into words, and length counts them.
Find the reason this R code fails to replace spaces with dashes in a string.
text <- "hello world"
new_text <- gsub(" ", "-", text)
print(new_text)Check the parentheses carefully in the function call.
The gsub call is missing a closing parenthesis, causing a syntax error.
Choose the code that extracts the first 3 letters from the string word <- "processing".
Remember R strings start at position 1, not 0.
substr(word, 1, 3) extracts characters from position 1 to 3 inclusive, giving 'pro'.
Given this R code, what is the number of words after removing punctuation?
text <- "Hello, world! Let's test text-processing."
clean_text <- gsub("[[:punct:]]", "", text)
words <- strsplit(clean_text, " ")[[1]]
length(words)Count words after punctuation is removed and text split by spaces.
After removing punctuation, the text becomes "Hello world Lets test textprocessing" which splits into 5 words.