0
0
R Programmingprogramming~10 mins

Why text processing is common in R Programming - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to print the number of words in the text.

R Programming
text <- "Hello world from R"
words <- strsplit(text, [1])[[1]]
print(length(words))
Drag options to blanks, or click blank then click option'
A" "
B"."
C","
D"\n"
Attempts:
3 left
💡 Hint
Common Mistakes
Using a comma or period as the separator instead of a space.
Forgetting to use quotes around the separator.
2fill in blank
medium

Complete the code to convert all text to lowercase.

R Programming
text <- "Hello World"
lower_text <- [1](text)
print(lower_text)
Drag options to blanks, or click blank then click option'
Atolower
Btrimws
Ccapitalize
Dtoupper
Attempts:
3 left
💡 Hint
Common Mistakes
Using toupper() which converts text to uppercase.
Using functions unrelated to case conversion.
3fill in blank
hard

Fix the error in the code to replace all spaces with underscores.

R Programming
text <- "Hello world from R"
new_text <- gsub([1], "_", text)
print(new_text)
Drag options to blanks, or click blank then click option'
A"-"
B"_"
C" "
D"."
Attempts:
3 left
💡 Hint
Common Mistakes
Replacing underscores instead of spaces.
Using the wrong pattern string.
4fill in blank
hard

Fill both blanks to create a named vector with word lengths for words longer than 3 characters.

R Programming
words <- c("cat", "elephant", "dog", "giraffe")
lengths <- c([1](words))
names(lengths) <- words
filtered <- lengths[lengths [2] 3]
print(filtered)
Drag options to blanks, or click blank then click option'
Anchar
Blength
C>
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using length() which returns the number of elements, not characters.
Using the wrong comparison operator.
5fill in blank
hard

Fill all three blanks to create a named vector of uppercase words longer than 4 characters.

R Programming
words <- c("apple", "bat", "carrot", "dog")
upper_words <- toupper([1])
lengths <- nchar([2])
result <- upper_words[lengths [3] 4]
print(result)
Drag options to blanks, or click blank then click option'
Awords
Bupper_words
C>
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using upper_words for length calculation which is already uppercase.
Using the wrong comparison operator.