Vector indexing (1-based) in R Programming - Time & Space Complexity
We want to understand how long it takes to get elements from a vector in R.
How does the time change when we ask for one or many elements?
Analyze the time complexity of the following code snippet.
# Create a vector of numbers
vec <- 1:1000
# Access a single element
x <- vec[500]
# Access multiple elements
y <- vec[c(10, 20, 30)]
This code gets elements from a vector by their position using 1-based indexing.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Accessing elements by index in the vector.
- How many times: Once for each element requested (single or multiple).
Getting one element takes about the same time no matter the vector size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 for single element, 3 for three elements |
| 100 | 1 for single element, 3 for three elements |
| 1000 | 1 for single element, 3 for three elements |
Pattern observation: Time grows with how many elements you ask for, not the total vector size.
Time Complexity: O(k)
This means the time depends on how many elements you access, not the whole vector size.
[X] Wrong: "Accessing an element takes longer if the vector is bigger."
[OK] Correct: Vector indexing in R directly jumps to the element, so size does not slow it down.
Knowing how vector access time works helps you write faster code and explain your choices clearly.
"What if we accessed elements using a loop instead of all at once? How would the time complexity change?"
Practice
v when using indexing like v[1]?Solution
Step 1: Understand R vector indexing
R vectors start indexing at 1, not 0 like some other languages.Step 2: Apply indexing to vector
Usingvv[1]selects the first element of the vector.Final Answer:
The first element (position 1) -> Option AQuick Check:
R indexing starts at 1 = first element [OK]
- 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
vec in R?Solution
Step 1: Recall R vector indexing syntax
In R, square brackets[]are used to access elements by position.Step 2: Identify correct syntax for third element
Usingvec[3]correctly accesses the third element.Final Answer:
vec[3] -> Option AQuick Check:
Use square brackets for indexing in R [OK]
- Using parentheses () instead of square brackets
- Using curly braces {} which are invalid for indexing
- Using angle brackets <> which are not valid syntax
nums <- c(10, 20, 30, 40, 50) nums[2:4]
Solution
Step 1: Understand the vector and indexing range
The vectornumshas elements 10, 20, 30, 40, 50 at positions 1 to 5.Step 2: Extract elements from position 2 to 4
Usingnums[2:4]selects elements at positions 2, 3, and 4 which are 20, 30, and 40.Final Answer:
[20 30 40] -> Option BQuick Check:
Positions 2 to 4 = 20, 30, 40 [OK]
- Including position 5 by mistake
- Starting range at 1 instead of 2
- Confusing vector values with positions
vals <- c(5, 10, 15, 20) print(vals[0])
Solution
Step 1: Identify the indexing error
In R, vector indexing starts at 1; index 0 is invalid and returns an empty vector.Step 2: Correct the index to start at 1
Replacevals[0]withvals[1]to access the first element.Final Answer:
Error: Index 0 is invalid; use index starting at 1 -> Option CQuick Check:
R indexing starts at 1, not 0 [OK]
- Assuming 0 is a valid index like in other languages
- Trying to use parentheses for indexing
- Thinking vector type causes the error
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?Solution
Step 1: Understand how to select multiple elements
In R, usec()inside square brackets to select multiple positions.Step 2: Apply correct indexing for positions 1, 3, and 5
Usingdata[c(1, 3, 5)]selects elements at these positions: 3, 9, and 15.Final Answer:
data[c(1, 3, 5)] -> Option DQuick Check:
Use c() inside [] to pick multiple positions [OK]
- Using colon with multiple steps incorrectly
- Using commas inside [] without c()
- Using zero-based indices like 0, 2, 4
Ready to prove it?
Pass the test and this pattern moves unknown
