Bird
Raised Fist0
R Programmingprogramming~20 mins

strsplit in R Programming - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Strsplit Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2: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)
A
[[1]]
[1] "apple" "banana" "cherry orange"
B
[[1]]
[1] "apple,banana;cherry orange"
C
[[1]]
[1] "apple" "banana;cherry" "orange"
D
[[1]]
[1] "apple" "banana" "cherry" "orange"
Attempts:
2 left
💡 Hint
The split argument can use regular expressions to split by multiple characters.
❓ Predict Output
intermediate
2: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)
A
[[1]]
[1] "one" "two" "three"
B
[[1]]
[1] "one.two.three"
C
[[1]]
[1] "one" "two.three"
D
[[1]]
[1] "one.two" "three"
Attempts:
2 left
💡 Hint
fixed = TRUE treats the split string literally, not as a regular expression.
🔧 Debug
advanced
2: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)
AError in strsplit: unused argument (split = c(",", ";"))
BError in strsplit: 'split' must be a single string
C
[[1]]
[1] "a" "b" "c"
DNULL
Attempts:
2 left
💡 Hint
The split argument must be a single string, not a vector.
🧠 Conceptual
advanced
1:30remaining
Understanding strsplit output structure
What is the type and structure of the value returned by strsplit in R?
AA list where each element is a character vector of split parts
BA character vector of split parts
CA data frame with columns for each split part
DA matrix of characters with rows as split parts
Attempts:
2 left
💡 Hint
strsplit always returns a list, even if input is a single string.
❓ Predict Output
expert
1: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]])
A1
B2
C3
DError: subscript out of bounds
Attempts:
2 left
💡 Hint
The first element corresponds to splitting the first string.

Practice

(1/5)
1. What does the strsplit function in R do?
easy
A. It joins multiple strings into one string.
B. It counts the number of characters in a string.
C. It converts a string to uppercase letters.
D. It breaks a string into parts based on a separator.

Solution

  1. Step 1: Understand the purpose of strsplit

    The strsplit function is designed to split a string into smaller parts using a specified separator.
  2. Step 2: Compare options with function behavior

    Only It breaks a string into parts based on a separator. correctly describes this behavior. Options A, C, and D describe different string functions.
  3. Final Answer:

    It breaks a string into parts based on a separator. -> Option D
  4. Quick Check:

    strsplit splits strings = B [OK]
Hint: Remember: strsplit splits strings by separator [OK]
Common Mistakes:
  • Confusing strsplit with paste or toupper functions
  • Thinking strsplit counts characters
  • Assuming strsplit joins strings
2. Which of the following is the correct syntax to split the string "apple,banana,orange" by commas using strsplit?
easy
A. strsplit("apple,banana,orange", " ")
B. strsplit("apple,banana,orange", ".")
C. strsplit("apple,banana,orange", ",")
D. strsplit("apple,banana,orange")

Solution

  1. Step 1: Identify the separator in the string

    The string uses commas "," to separate words.
  2. Step 2: Match the separator in strsplit syntax

    strsplit("apple,banana,orange", ",") uses "," as the separator, which is correct. strsplit("apple,banana,orange", ".") uses ".", C uses space, and D misses the separator argument.
  3. Final Answer:

    strsplit("apple,banana,orange", ",") -> Option C
  4. Quick Check:

    Separator matches comma = A [OK]
Hint: Use the exact separator character inside quotes [OK]
Common Mistakes:
  • Forgetting to provide the separator argument
  • Using wrong separator character
  • Using unquoted separator
3. What is the output of the following R code?
result <- strsplit("one-two-three", "-")
print(result)
medium
A. [[1]] "one" "two" "three"
B. "one-two-three"
C. [1] "one" "two" "three"
D. Error: invalid separator

Solution

  1. Step 1: Understand strsplit output type

    strsplit returns a list where each element is a character vector of split parts.
  2. Step 2: Analyze the printed output

    The print shows a list with one element ([[1]]) containing the split strings "one", "two", "three".
  3. Final Answer:

    [[1]] "one" "two" "three" -> Option A
  4. Quick Check:

    strsplit returns list of vectors = A [OK]
Hint: Remember strsplit returns a list, not a vector directly [OK]
Common Mistakes:
  • Expecting a simple vector instead of a list
  • Confusing output with a single string
  • Thinking strsplit throws an error for valid separator
4. Identify the error in this R code:
strsplit("cat,dog,bird", ",")[[2]]
medium
A. The string should be a vector, not a single string.
B. Index [[2]] is out of bounds because strsplit returns a list of length 1.
C. Missing parentheses around strsplit call.
D. Separator "," is incorrect for splitting.

Solution

  1. Step 1: Check the output structure of strsplit

    strsplit returns a list with one element per input string. Here, input is one string, so list length is 1.
  2. Step 2: Analyze the indexing [[2]]

    Trying to access [[2]] causes an out-of-bounds error because the list has only one element.
  3. Final Answer:

    Index [[2]] is out of bounds because strsplit returns a list of length 1. -> Option B
  4. Quick Check:

    List length 1, index 2 invalid = D [OK]
Hint: Check list length before using [[index]] [OK]
Common Mistakes:
  • Assuming strsplit returns multiple list elements for one string
  • Using wrong separator
  • Misunderstanding list indexing
5. You have a vector of strings: c("red:blue", "green:yellow", "black:white"). How do you split each string by ":" and get a flat vector of all colors?
hard
A. unlist(strsplit(c("red:blue", "green:yellow", "black:white"), ":"))
B. strsplit(c("red:blue", "green:yellow", "black:white"), ":")
C. paste(strsplit(c("red:blue", "green:yellow", "black:white"), ":"))
D. split(c("red:blue", "green:yellow", "black:white"), ":")

Solution

  1. Step 1: Use strsplit to split each string by ":"

    strsplit returns a list of character vectors for each element in the input vector.
  2. Step 2: Flatten the list to a single vector

    Using unlist converts the list of vectors into one flat character vector containing all colors.
  3. Final Answer:

    unlist(strsplit(c("red:blue", "green:yellow", "black:white"), ":")) -> Option A
  4. Quick Check:

    Use unlist to flatten strsplit output = C [OK]
Hint: Use unlist() to flatten strsplit list output [OK]
Common Mistakes:
  • Forgetting to unlist and getting a list instead of vector
  • Using paste which concatenates instead of splitting
  • Using split which is for factors, not strings
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard