Challenge - 5 Problems
Named Vector Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of named vector indexing
What is the output of this R code snippet?
R Programming
vec <- c(a = 10, b = 20, c = 30) result <- vec["b"] print(result)
Attempts:
2 left
💡 Hint
Remember that named vectors keep their names when indexed by name.
✗ Incorrect
Indexing a named vector by name returns a named vector of length 1, so the output includes the name and the value.
❓ Predict Output
intermediate2:00remaining
Length of named vector after subsetting
What is the length of the vector after this operation?
R Programming
vec <- c(x = 5, y = 15, z = 25) sub_vec <- vec[c("x", "z")] length(sub_vec)
Attempts:
2 left
💡 Hint
Subsetting by a vector of names returns those elements.
✗ Incorrect
Subsetting by two names returns a vector of length 2 containing those elements.
❓ Predict Output
advanced2:00remaining
Output of modifying named vector elements
What is the output of this code?
R Programming
vec <- c(a = 1, b = 2, c = 3) vec["b"] <- 10 print(vec)
Attempts:
2 left
💡 Hint
You can assign new values to named vector elements by name.
✗ Incorrect
Assigning to a named element changes its value but keeps the name.
❓ Predict Output
advanced2:00remaining
Result of unnamed indexing on named vector
What will this code print?
R Programming
vec <- c(one = 100, two = 200, three = 300) print(vec[2])
Attempts:
2 left
💡 Hint
Indexing by position returns a named vector element.
✗ Incorrect
Indexing by position returns the element with its name.
❓ Predict Output
expert3:00remaining
Output of combining named vectors with overlapping names
What is the output of this R code?
R Programming
v1 <- c(a = 1, b = 2) v2 <- c(b = 3, c = 4) combined <- c(v1, v2) print(combined)
Attempts:
2 left
💡 Hint
Combining vectors concatenates elements and keeps all names, even duplicates.
✗ Incorrect
Concatenating named vectors keeps all elements and names, including duplicates.