Challenge - 5 Problems
Strsplit Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of strsplit with multiple delimiters
What is the output of the following R code?
R Programming
text <- "apple,banana;cherry orange" result <- strsplit(text, split = ",|;| ") print(result)
Attempts:
2 left
💡 Hint
The split argument can use regular expressions to split by multiple characters.
✗ Incorrect
The split pattern ",|;| " means split at commas, semicolons, or spaces. So the string is split into four parts: apple, banana, cherry, and orange.
❓ Predict Output
intermediate2:00remaining
Result of strsplit with fixed = TRUE
What is the output of this R code?
R Programming
text <- "one.two.three" result <- strsplit(text, split = ".", fixed = TRUE) print(result)
Attempts:
2 left
💡 Hint
fixed = TRUE treats the split string literally, not as a regular expression.
✗ Incorrect
With fixed = TRUE, the dot is treated as a normal character, so the string splits at each dot into three parts.
🔧 Debug
advanced2:00remaining
Identify the error in strsplit usage
What error does this R code produce?
R Programming
text <- "a,b,c" result <- strsplit(text, split = c(",", ";")) print(result)
Attempts:
2 left
💡 Hint
The split argument must be a single string, not a vector.
✗ Incorrect
strsplit expects a single string pattern for splitting. Passing a vector causes an error about 'split' needing to be a single string.
🧠 Conceptual
advanced1:30remaining
Understanding strsplit output structure
What is the type and structure of the value returned by strsplit in R?
Attempts:
2 left
💡 Hint
strsplit always returns a list, even if input is a single string.
✗ Incorrect
strsplit returns a list. Each element corresponds to an input string and contains a character vector of the split parts.
❓ Predict Output
expert1:30remaining
Count of elements after splitting multiple strings
What is the length of the vector inside the first element of the list returned by this code?
R Programming
texts <- c("red,green,blue", "circle,square") result <- strsplit(texts, split = ",") length(result[[1]])
Attempts:
2 left
💡 Hint
The first element corresponds to splitting the first string.
✗ Incorrect
The first string "red,green,blue" splits into three parts, so length(result[[1]]) is 3.