Modifying and adding elements in R Programming - Time & Space Complexity
When we change or add items in a list or vector, the time it takes can grow as the list grows.
We want to know how the work changes when the list gets bigger.
Analyze the time complexity of the following code snippet.
my_vector <- 1:5
my_vector[3] <- 10 # Modify element at position 3
my_vector <- c(my_vector, 20) # Add element at the end
This code changes one element in a vector and then adds a new element at the end.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Modifying an element and adding an element to a vector.
- How many times: Each operation happens once here, but adding can involve copying the whole vector.
When you change one element, it takes about the same time no matter the size.
But when you add an element, R often makes a new bigger vector and copies all old elements, so time grows with size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 copy steps when adding |
| 100 | About 100 copy steps when adding |
| 1000 | About 1000 copy steps when adding |
Pattern observation: Modifying is quick and steady; adding grows linearly with size because of copying.
Time Complexity: O(n)
This means adding an element takes time that grows in a straight line with the vector size, while modifying is fast and steady.
[X] Wrong: "Adding an element to a vector is always fast and takes the same time no matter what."
[OK] Correct: Because R often copies the whole vector to add an element, so bigger vectors take more time to add to.
Understanding how adding and changing elements affects time helps you write faster code and explain your choices clearly.
"What if we used a list instead of a vector? How would the time complexity for adding elements change?"
Practice
v to 10?Solution
Step 1: Understand element access in R vectors
Elements in R vectors are accessed using square brackets [ ] with the position number inside.Step 2: Assign new value to the element
Use the assignment operator <- to set the new value at the specified position.Final Answer:
v[2] <- 10 -> Option DQuick Check:
Use [ ] and <- to modify elements [OK]
- Using parentheses instead of square brackets
- Adding parentheses after the value
- Using curly braces for indexing
v in R?Solution
Step 1: Understand how to add elements to a vector
In R, use thec()function to combine vectors or add elements.Step 2: Combine existing vector with new element
Usev <- c(v, 5)to add 5 at the end of vectorv.Final Answer:
v <- c(v, 5) -> Option BQuick Check:
Use c() to add elements [OK]
- Using + operator to add elements
- Incorrect indexing syntax
- Adding vectors without assignment
v <- c(1, 2, 3) v[1] <- 10 v <- c(v, 4) v
Solution
Step 1: Modify first element of vector
The first line changes the first element from 1 to 10, so vector becomes (10, 2, 3).Step 2: Add new element 4 at the end
Using c(v, 4) adds 4 to the end, resulting in (10, 2, 3, 4).Final Answer:
[10, 2, 3, 4] -> Option CQuick Check:
Modify then add element = [10, 2, 3, 4] [OK]
- Forgetting to assign after adding element
- Not updating the first element correctly
- Confusing vector output format
v:v <- c(1, 2, 3) v + 7
Solution
Step 1: Understand vector addition vs adding elements
Usingv + 7adds 7 to each element, but does not append a new element.Step 2: Check for assignment to update vector
The code does not assign the result back tov, so vector remains unchanged. Also, to add an element,c()should be used.Final Answer:
The code adds 7 to each element, not appends 7 -> Option AQuick Check:
Adding elements needs c() and assignment [OK]
- Using + to append elements
- Not assigning result back to vector
- Confusing element-wise addition with adding elements
v <- c(2, 4, 6), which code correctly replaces the last element with 8 and then adds 10 at the end?Solution
Step 1: Replace last element using length()
Usev[length(v)] <- 8to change the last element (position 3) to 8.Step 2: Add 10 at the end with c()
Usev <- c(v, 10)to append 10 to the vector.Final Answer:
v[length(v)] <- 8; v <- c(v, 10) -> Option AQuick Check:
Use length() to index last element, c() to add [OK]
- Using negative index incorrectly
- Adding element with + operator
- Assigning to length(v)+1 without c()
Ready to prove it?
Pass the test and this pattern moves unknown
