Recall & Review
beginner
What does the
nchar() function do in R?It returns the number of characters in a string, including spaces and special characters.
Click to reveal answer
beginner
How do you extract a part of a string in R?
You use the
substring() function, specifying the start and end positions of the part you want.Click to reveal answer
beginner
Example: What is the output of
nchar("Hello World")?The output is 11 because there are 11 characters including the space.
Click to reveal answer
beginner
Example: What does
substring("Programming", 2, 5) return?It returns
"rogr", which are characters from position 2 to 5.Click to reveal answer
intermediate
Can
substring() extract from the middle to the end of a string without specifying the end?Yes, if you omit the end position, it extracts from the start position to the end of the string.
Click to reveal answer
What does
nchar("R is fun") return?✗ Incorrect
The string "R is fun" has 8 characters including spaces.
What is the result of
substring("Data", 2, 3)?✗ Incorrect
Characters at positions 2 and 3 are "a" and "t", so the result is "at".
If you want to get the last 3 characters of a string "Example", which is the correct call?
✗ Incorrect
Positions 5 to 7 correspond to the last 3 characters "ple".
What happens if
substring() start position is greater than string length?✗ Incorrect
If start position is beyond string length, substring returns an empty string.
Which function would you use to find how many characters are in a string?
✗ Incorrect
nchar() returns the number of characters in a string.Explain how to use
nchar() and substring() to find the length of a string and extract a part of it.Think about counting letters and cutting out a piece of a word.
You got /3 concepts.
Describe what happens if you use
substring() with only a start position and no end position.Imagine cutting a string from a point to the end.
You got /3 concepts.