Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to split the string 'apple,banana,cherry' by commas.
R Programming
result <- strsplit("apple,banana,cherry", [1])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong separator like a semicolon or space.
Not putting the separator inside quotes.
✗ Incorrect
The strsplit function splits a string by the specified separator. Here, the separator is a comma, so we use ",".
2fill in blank
mediumComplete the code to split the string 'one|two|three' by the pipe character.
R Programming
result <- strsplit("one|two|three", [1])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using just "|" without escaping causes unexpected splitting.
Using "\\\\|" which overescapes in R strings.
✗ Incorrect
In R, the pipe character '|' is a special symbol in regular expressions, so it must be escaped with "\\|" to be used as a literal separator.
3fill in blank
hardFix the error in splitting the string 'a,b;c' by commas and semicolons.
R Programming
result <- strsplit("a,b;c", [1])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string with both separators without brackets.
Using separators without escaping special characters.
✗ Incorrect
To split by multiple characters like comma and semicolon, use a regular expression with square brackets: "[,;]".
4fill in blank
hardFill both blanks to split the string 'red|green|blue' by pipe and get the first element.
R Programming
parts <- strsplit("red|green|blue", [1]) first_color <- parts[[1]][2]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Not escaping the pipe character correctly.
Using single brackets [1] which returns a list, not a vector.
✗ Incorrect
The pipe character must be escaped as "\\|" in strsplit. The result is a list, so to get the first element use [[1]].
5fill in blank
hardFill all three blanks to split 'cat,dog;bird' by commas or semicolons and get the second word.
R Programming
words <- strsplit("cat,dog;bird", [1]) second_word <- words[2][3]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using single brackets [1] instead of double brackets [[1]] to extract list element.
Not using the correct regex pattern for multiple separators.
✗ Incorrect
Use "[,;]" to split by comma or semicolon. The result is a list, so use [[1]] to get the vector, then [2] to get the second word.