Bird
Raised Fist0
R Programmingprogramming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. In R, what is the position of the first element in a vector v when using indexing like v[1]?
easy
A. The first element (position 1)
B. The zeroth element (position 0)
C. The last element
D. An error because indexing starts at 0

Solution

  1. Step 1: Understand R vector indexing

    R vectors start indexing at 1, not 0 like some other languages.
  2. Step 2: Apply indexing to vector v

    Using v[1] selects the first element of the vector.
  3. Final Answer:

    The first element (position 1) -> Option A
  4. Quick Check:

    R indexing starts at 1 = first element [OK]
Hint: Remember: R indexing always starts at 1, never 0 [OK]
Common Mistakes:
  • Thinking indexing starts at 0 like in Python or C
  • Confusing position 1 with last element
  • Assuming indexing causes an error if 0 is used
2. Which of the following is the correct syntax to get the third element from a vector vec in R?
easy
A. vec[3]
B. vec<3>
C. vec{3}
D. vec(3)

Solution

  1. Step 1: Recall R vector indexing syntax

    In R, square brackets [] are used to access elements by position.
  2. Step 2: Identify correct syntax for third element

    Using vec[3] correctly accesses the third element.
  3. Final Answer:

    vec[3] -> Option A
  4. Quick Check:

    Use square brackets for indexing in R [OK]
Hint: Use square brackets [] to index vectors in R [OK]
Common Mistakes:
  • Using parentheses () instead of square brackets
  • Using curly braces {} which are invalid for indexing
  • Using angle brackets <> which are not valid syntax
3. What will be the output of the following R code?
nums <- c(10, 20, 30, 40, 50)
nums[2:4]
medium
A. [10 20 30]
B. [20 30 40]
C. [20 30 40 50]
D. [30 40 50]

Solution

  1. Step 1: Understand the vector and indexing range

    The vector nums has elements 10, 20, 30, 40, 50 at positions 1 to 5.
  2. Step 2: Extract elements from position 2 to 4

    Using nums[2:4] selects elements at positions 2, 3, and 4 which are 20, 30, and 40.
  3. Final Answer:

    [20 30 40] -> Option B
  4. Quick Check:

    Positions 2 to 4 = 20, 30, 40 [OK]
Hint: Use colon : to select a range of positions [OK]
Common Mistakes:
  • Including position 5 by mistake
  • Starting range at 1 instead of 2
  • Confusing vector values with positions
4. Consider the R code below. What is the error and how to fix it?
vals <- c(5, 10, 15, 20)
print(vals[0])
medium
A. Error: Use parentheses instead of brackets
B. No error; prints 5
C. Error: Index 0 is invalid; use index starting at 1
D. Error: Vector must be numeric

Solution

  1. Step 1: Identify the indexing error

    In R, vector indexing starts at 1; index 0 is invalid and returns an empty vector.
  2. Step 2: Correct the index to start at 1

    Replace vals[0] with vals[1] to access the first element.
  3. Final Answer:

    Error: Index 0 is invalid; use index starting at 1 -> Option C
  4. Quick Check:

    R indexing starts at 1, not 0 [OK]
Hint: Never use 0 as index in R vectors [OK]
Common Mistakes:
  • Assuming 0 is a valid index like in other languages
  • Trying to use parentheses for indexing
  • Thinking vector type causes the error
5. You have a vector data <- c(3, 6, 9, 12, 15). How do you create a new vector containing only the 1st, 3rd, and 5th elements using vector indexing in R?
hard
A. data[1, 3, 5]
B. data[1:3:5]
C. data[c(0, 2, 4)]
D. data[c(1, 3, 5)]

Solution

  1. Step 1: Understand how to select multiple elements

    In R, use c() inside square brackets to select multiple positions.
  2. Step 2: Apply correct indexing for positions 1, 3, and 5

    Using data[c(1, 3, 5)] selects elements at these positions: 3, 9, and 15.
  3. Final Answer:

    data[c(1, 3, 5)] -> Option D
  4. Quick Check:

    Use c() inside [] to pick multiple positions [OK]
Hint: Use c() inside [] to select multiple indices [OK]
Common Mistakes:
  • Using colon with multiple steps incorrectly
  • Using commas inside [] without c()
  • Using zero-based indices like 0, 2, 4
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard