0
0
R Programmingprogramming~20 mins

Named vectors in R Programming - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Named Vector Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
Ac 30
Bb 20
C20
DError: object 'b' not found
Attempts:
2 left
💡 Hint
Remember that named vectors keep their names when indexed by name.
Predict Output
intermediate
2: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)
A1
B3
C2
DError: invalid subscript type
Attempts:
2 left
💡 Hint
Subsetting by a vector of names returns those elements.
Predict Output
advanced
2: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)
A
a 1
b 10
c 3
B1 10 3
C
a 1
b 2
c 3
DError: cannot assign to a named vector element
Attempts:
2 left
💡 Hint
You can assign new values to named vector elements by name.
Predict Output
advanced
2: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])
Aone 100
B200
CError: subscript out of bounds
Dtwo 200
Attempts:
2 left
💡 Hint
Indexing by position returns a named vector element.
Predict Output
expert
3: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)
A
a 1
b 2
b 3
c 4
B
a 1
b 5
c 4
C
a 1
b 3
c 4
DError: duplicate names not allowed
Attempts:
2 left
💡 Hint
Combining vectors concatenates elements and keeps all names, even duplicates.