Complete the code to find the number of characters in the string.
length <- [1]("Hello World")
substring instead of nchar.length which counts elements in a vector, not characters.The nchar() function returns the number of characters in a string in R.
Complete the code to extract the first 5 characters from the string.
part <- [1]("Programming", 1, 5)
nchar which counts characters, not extracts.str_sub which is from a different package.The substring() function extracts a substring from a string given start and end positions.
Fix the error in the code to correctly extract characters 3 to 7 from the string.
result <- substring("DataScience", [1], 7)
The start position should be 3 to extract characters from position 3 to 7.
Fill both blanks to create a dictionary of word lengths for words longer than 3 characters.
lengths <- list([1] = nchar(word) for word in words if nchar(word) [2] 3)
== which selects words exactly 3 characters long.< which selects shorter words.We use word as the key and check if word length is greater than 3 with >.
Fill all three blanks to create a named vector of substring results for words longer than 4 characters.
result <- c([1] = substring(word, 1, 3) for word in words if nchar(word) [2] 4 and word [3] "Data")
We use word as the name, select words with length greater than 4, and exclude the word "Data" using !=.