Challenge - 5 Problems
Vector Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of combining numeric and character vectors
What is the output of this R code when combining numeric and character vectors?
R Programming
v1 <- c(1, 2, 3) v2 <- c("a", "b", "c") v3 <- c(v1, v2) print(v3)
Attempts:
2 left
💡 Hint
Remember that combining numeric and character vectors converts all elements to characters.
✗ Incorrect
When you combine numeric and character vectors in R, all elements are coerced to character type. So the numeric values become strings.
❓ Predict Output
intermediate1:30remaining
Length of a mixed vector
What is the length of the vector created by this code?
R Programming
v1 <- c(10, 20, 30) v2 <- c("x", "y") v3 <- c(v1, v2) length(v3)
Attempts:
2 left
💡 Hint
The length counts all elements after combining.
✗ Incorrect
The vector v3 combines 3 numeric and 2 character elements, so length is 5.
🔧 Debug
advanced2:00remaining
Error in vector creation with mixed types
What error does this R code produce?
R Programming
v <- c(1, "two", TRUE, 4.5) print(v)
Attempts:
2 left
💡 Hint
Check how R coerces different types in a vector.
✗ Incorrect
R coerces all elements to the most flexible type, here character, so no error occurs.
🧠 Conceptual
advanced1:30remaining
Understanding vector coercion priority
Which type will the vector have after combining these elements: numeric, logical, and character?
Attempts:
2 left
💡 Hint
R coerces to the most flexible type to hold all values.
✗ Incorrect
Character is the most flexible type among logical, numeric, and character, so all elements become characters.
❓ Predict Output
expert2:00remaining
Output of vector recycling in arithmetic
What is the output of this R code?
R Programming
v1 <- c(1, 2, 3, 4) v2 <- c(10, 20) v3 <- v1 + v2 print(v3)
Attempts:
2 left
💡 Hint
Remember how R recycles shorter vectors in arithmetic operations.
✗ Incorrect
v2 is recycled to match length of v1: (10,20,10,20). Adding element-wise gives (11,22,13,24).