Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
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.
✗ Incorrect
The text is split by spaces to separate words, so the delimiter is a space " ".
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using toupper() which converts text to uppercase.
Using functions unrelated to case conversion.
✗ Incorrect
tolower() converts all characters in the text to lowercase.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Replacing underscores instead of spaces.
Using the wrong pattern string.
✗ Incorrect
To replace spaces, the pattern to match is a space " ".
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using length() which returns the number of elements, not characters.
Using the wrong comparison operator.
✗ Incorrect
nchar() gets the length of each word, and we filter lengths greater than 3.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using upper_words for length calculation which is already uppercase.
Using the wrong comparison operator.
✗ Incorrect
We convert original words to uppercase, get their lengths, and filter those longer than 4.