Complete the code to print the number of words in the text.
text <- "Hello world from R" words <- strsplit(text, [1])[[1]] print(length(words))
The text is split by spaces to separate words, so the delimiter is a space " ".
Complete the code to convert all text to lowercase.
text <- "Hello World" lower_text <- [1](text) print(lower_text)
tolower() converts all characters in the text to lowercase.
Fix the error in the code to replace all spaces with underscores.
text <- "Hello world from R" new_text <- gsub([1], "_", text) print(new_text)
To replace spaces, the pattern to match is a space " ".
Fill both blanks to create a named vector with word lengths for words longer than 3 characters.
words <- c("cat", "elephant", "dog", "giraffe") lengths <- c([1](words)) names(lengths) <- words filtered <- lengths[lengths [2] 3] print(filtered)
nchar() gets the length of each word, and we filter lengths greater than 3.
Fill all three blanks to create a named vector of uppercase words longer than 4 characters.
words <- c("apple", "bat", "carrot", "dog") upper_words <- toupper([1]) lengths <- nchar([2]) result <- upper_words[lengths [3] 4] print(result)
We convert original words to uppercase, get their lengths, and filter those longer than 4.
