Challenge - 5 Problems
Vector Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of combining different types with c()
What is the output of this R code?
x <- c(1, TRUE, "3", 4.5)
print(x)
R Programming
x <- c(1, TRUE, "3", 4.5) print(x)
Attempts:
2 left
💡 Hint
Remember that c() converts all elements to the most flexible type to keep them in one vector.
✗ Incorrect
In R, when combining different types with c(), all elements are coerced to a common type. Here, numeric and logical values are converted to character because of the presence of a character "3". TRUE becomes "TRUE" as character.
❓ Predict Output
intermediate1:30remaining
Length of vector created with c()
What is the length of the vector created by this code?
v <- c(NA, NULL, 5, c(2, 3))
length(v)
R Programming
v <- c(NA, NULL, 5, c(2, 3)) length(v)
Attempts:
2 left
💡 Hint
Remember that NULL is ignored inside c(), but NA counts as an element.
✗ Incorrect
NULL is dropped when combined with c(), so the vector is c(NA, 5, 2, 3) which has length 4.
🔧 Debug
advanced1:30remaining
Why does this c() call produce an error?
This code produces an error. What is the cause?
v <- c(1, 2, 3, , 5)
R Programming
v <- c(1, 2, 3, , 5)
Attempts:
2 left
💡 Hint
Check the commas carefully inside the c() function.
✗ Incorrect
There is a missing value between two commas, which is a syntax error in R.
🧠 Conceptual
advanced1:30remaining
Behavior of c() with named elements
What is the output of this R code?
v <- c(a=1, b=2, 3)
names(v)
R Programming
v <- c(a=1, b=2, 3) names(v)
Attempts:
2 left
💡 Hint
Only elements explicitly named get names; unnamed elements have empty names.
✗ Incorrect
The first two elements have names "a" and "b". The third element is unnamed, so its name is an empty string "".
❓ Predict Output
expert2:00remaining
Output of nested c() calls with mixed types
What is the output of this R code?
v <- c(1, c(TRUE, "text"), 3.5)
print(v)
R Programming
v <- c(1, c(TRUE, "text"), 3.5) print(v)
Attempts:
2 left
💡 Hint
Remember that c() flattens nested vectors and coerces types to the most flexible one.
✗ Incorrect
The inner c(TRUE, "text") coerces TRUE to character "TRUE". Then the outer c() flattens and coerces all to character because of "text". 1 becomes "1", 3.5 becomes "3.5", "TRUE" and "text" remain.