0
0
R Programmingprogramming~5 mins

nchar and substring in R Programming - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
A7
B8
C9
D6
What is the result of substring("Data", 2, 3)?
A"at"
B"Da"
C"ta"
D"a"
If you want to get the last 3 characters of a string "Example", which is the correct call?
Asubstring("Example", 6, 8)
Bsubstring("Example", 4, 7)
Csubstring("Example", 5, 7)
Dsubstring("Example", 5)
What happens if substring() start position is greater than string length?
AReturns empty string
BReturns whole string
CReturns error
DReturns NULL
Which function would you use to find how many characters are in a string?
Asubstring()
Bstr_length()
Clength()
Dnchar()
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.