0
0
R Programmingprogramming~10 mins

nchar and substring in R Programming - Interactive Code Practice

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

Complete the code to find the number of characters in the string.

R Programming
length <- [1]("Hello World")
Drag options to blanks, or click blank then click option'
Asubstring
Bnchar
Clength
Dstr_length
Attempts:
3 left
💡 Hint
Common Mistakes
Using substring instead of nchar.
Using length which counts elements in a vector, not characters.
2fill in blank
medium

Complete the code to extract the first 5 characters from the string.

R Programming
part <- [1]("Programming", 1, 5)
Drag options to blanks, or click blank then click option'
Asubstring
Bstr_sub
Csubstr
Dnchar
Attempts:
3 left
💡 Hint
Common Mistakes
Using nchar which counts characters, not extracts.
Using str_sub which is from a different package.
3fill in blank
hard

Fix the error in the code to correctly extract characters 3 to 7 from the string.

R Programming
result <- substring("DataScience", [1], 7)
Drag options to blanks, or click blank then click option'
A1
B7
C0
D3
Attempts:
3 left
💡 Hint
Common Mistakes
Using 7 as start position which extracts only one character.
Using 0 which is invalid in R string indexing.
4fill in blank
hard

Fill both blanks to create a dictionary of word lengths for words longer than 3 characters.

R Programming
lengths <- list([1] = nchar(word) for word in words if nchar(word) [2] 3)
Drag options to blanks, or click blank then click option'
Aword
B>
C==
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using == which selects words exactly 3 characters long.
Using < which selects shorter words.
5fill in blank
hard

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

R Programming
result <- c([1] = substring(word, 1, 3) for word in words if nchar(word) [2] 4 and word [3] "Data")
Drag options to blanks, or click blank then click option'
Aword
B>
C!=
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using == instead of != which would include only "Data".
Using < instead of > which selects shorter words.