0
0
R Programmingprogramming~20 mins

Vector indexing (1-based) in R Programming - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Vector Indexing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
1:00remaining
What is the output of this R code using 1-based indexing?
Consider the vector v <- c(10, 20, 30, 40, 50). What is the output of v[3]?
R Programming
v <- c(10, 20, 30, 40, 50)
v[3]
A[1] 30
B[1] 20
C[1] 40
D[1] 10
Attempts:
2 left
💡 Hint
Remember R vectors start counting at 1, not 0.
Predict Output
intermediate
1:00remaining
What does this R code output when indexing with a vector?
Given v <- c(5, 10, 15, 20, 25), what is the output of v[c(2, 4)]?
R Programming
v <- c(5, 10, 15, 20, 25)
v[c(2, 4)]
A[1] 10 20
B[1] 5 15
C[1] 15 25
D[1] 20 25
Attempts:
2 left
💡 Hint
Indexing with a vector returns elements at those positions.
🧠 Conceptual
advanced
1:00remaining
What error does this R code raise when indexing with zero?
What happens when you run v <- c(1,2,3); v[0] in R?
R Programming
v <- c(1,2,3)
v[0]
AThrows an error: subscript out of bounds
BReturns the first element of v
CReturns an empty vector with no elements
DReturns NULL
Attempts:
2 left
💡 Hint
In R, zero is a special index that means no elements.
Predict Output
advanced
1:30remaining
What is the output of negative indexing in R?
Given v <- c(2, 4, 6, 8, 10), what does v[-c(1,3)] return?
R Programming
v <- c(2, 4, 6, 8, 10)
v[-c(1,3)]
A[1] 2 6 8 10
B[1] 4 8 10
C[1] 2 4 6
D[1] 4 6 8 10
Attempts:
2 left
💡 Hint
Negative indices exclude those positions from the result.
Predict Output
expert
1:30remaining
What is the output of this complex vector indexing in R?
Given v <- c(3, 6, 9, 12, 15), what is the output of v[c(TRUE, FALSE, TRUE, FALSE, TRUE)]?
R Programming
v <- c(3, 6, 9, 12, 15)
v[c(TRUE, FALSE, TRUE, FALSE, TRUE)]
A[1] 6 12
B[1] 15
C[1] 3 6 9
D[1] 3 9 15
Attempts:
2 left
💡 Hint
Logical vectors select elements where TRUE appears.