Challenge - 5 Problems
R String Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this R code?
Consider the following R code snippet. What will it print?
R Programming
text <- "Hello, R!" cat(substr(text, 8, 9))
Attempts:
2 left
💡 Hint
Remember that substr extracts characters from start to end positions inclusive.
✗ Incorrect
The substr function extracts characters from position 8 to 9. In "Hello, R!", position 8 is 'R' and position 9 is '!'. But cat prints without quotes and no newline by default, so it prints 'R!'. Option A is 'R' only, so the correct answer is A.
❓ Predict Output
intermediate2:00remaining
What does this R code print?
What is the output of this R code?
R Programming
text <- "Data123" print(grepl("[0-9]", text))
Attempts:
2 left
💡 Hint
grepl returns TRUE if the pattern is found anywhere in the string.
✗ Incorrect
The pattern "[0-9]" matches any digit. Since "Data123" contains digits, grepl returns TRUE.
🔧 Debug
advanced2:00remaining
Why does this R code produce an error?
This code is intended to concatenate two strings with a space. Why does it produce an error?
R Programming
str1 <- "Hello" str2 <- 123 result <- paste(str1, str2, sep=" ") print(result)
Attempts:
2 left
💡 Hint
Check how paste handles numeric arguments.
✗ Incorrect
paste automatically converts numeric arguments to strings, so no error occurs. The output is "Hello 123".
❓ Predict Output
advanced2:00remaining
What is the output of this R code involving string splitting?
What does this R code print?
R Programming
text <- "apple,banana,carrot" result <- strsplit(text, ",")[[1]] print(length(result))
Attempts:
2 left
💡 Hint
strsplit returns a list; extracting the first element gives the vector of split strings.
✗ Incorrect
strsplit splits the string into c("apple", "banana", "carrot"), so length is 3.
🧠 Conceptual
expert2:00remaining
Which option correctly explains the behavior of R's character vectors?
Which statement about character vectors in R is TRUE?
Attempts:
2 left
💡 Hint
Think about how strings behave in R when you try to change a single character.
✗ Incorrect
In R, strings inside character vectors are immutable; you cannot change a single character inside a string element directly.