Named vectors in R Programming - Time & Space Complexity
We want to understand how the time it takes to work with named vectors changes as the vector gets bigger.
Specifically, how does looking up or accessing elements by name affect the time?
Analyze the time complexity of the following code snippet.
# Create a named vector
vec <- c(a=10, b=20, c=30, d=40, e=50)
# Access element by name
value <- vec["c"]
# Access multiple elements by names
values <- vec[c("a", "e")]
# Add a new named element
vec["f"] <- 60
This code creates a named vector, accesses elements by their names, and adds a new named element.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Searching for names in the vector's name list to find matching elements.
- How many times: Once per element accessed by name; if multiple names are accessed, the search repeats for each.
When the vector is small, finding a name is quick. As the vector grows, searching through names takes longer.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 checks to find a name |
| 100 | About 100 checks to find a name |
| 1000 | About 1000 checks to find a name |
Pattern observation: The time to find a name grows roughly in direct proportion to the vector size.
Time Complexity: O(n)
This means the time to find an element by name grows linearly with the number of elements in the vector.
[X] Wrong: "Accessing elements by name is instant no matter how big the vector is."
[OK] Correct: The program must look through the names to find a match, so bigger vectors take more time.
Understanding how named vectors work helps you reason about data lookup speed, a key skill in many programming tasks.
"What if we changed the vector to a named list instead? How would the time complexity for accessing elements by name change?"
Practice
Solution
Step 1: Understand vector naming
Named vectors have names assigned to each element, making it easier to access elements by name.Step 2: Compare options
Only A vector where each element has a name label for easy access correctly describes a named vector as having name labels for elements.Final Answer:
A vector where each element has a name label for easy access -> Option BQuick Check:
Named vector = vector with names [OK]
- Thinking named vectors only hold numbers
- Confusing named vectors with immutable vectors
- Assuming named vectors store only strings
Solution
Step 1: Recall named vector syntax
Named vectors are created using c(name = value) pairs.Step 2: Check each option
c(apple = 5, banana = 3, cherry = 7) uses correct syntax: c(apple = 5, banana = 3, cherry = 7). Others use invalid syntax or functions.Final Answer:
c(apple = 5, banana = 3, cherry = 7) -> Option AQuick Check:
Use c(name = value) to create named vectors [OK]
- Using colons instead of equals for naming
- Trying to use non-existent functions like named_vector()
- Placing values before names incorrectly
fruits <- c(apple = 10, banana = 5, cherry = 8) fruits["banana"]
Solution
Step 1: Understand named vector indexing
Accessing a named vector element by name returns a named vector of length 1 with that element.Step 2: Analyze the code output
fruits["banana"] returns a named vector with name 'banana' and value 5, shown as c(banana = 5).Final Answer:
c(banana = 5) -> Option DQuick Check:
Access by name returns named vector element [OK]
- Expecting just the value without name
- Confusing output with string name
- Assuming it causes an error
numbers <- c(1 = "one", 2 = "two", 3 = "three")
Solution
Step 1: Check named vector syntax
Named vectors use name = value pairs, with names on the left and values on the right.Step 2: Analyze the code
The code uses numbers on the left and strings on the right, which reverses the expected order.Final Answer:
Values and names are reversed; names should be strings on the left -> Option AQuick Check:
Names = values, not values = names [OK]
- Putting values on left and names on right
- Thinking numbers cannot be names (they can as strings)
- Believing c() can't create named vectors
temps <- c(Mon = 22, Tue = 25, Wed = 20, Thu = 23), how would you create a new named vector with only days where temperature is above 22?Solution
Step 1: Understand logical indexing with named vectors
You can use a logical condition inside square brackets to filter elements.Step 2: Apply condition temps > 22
temps > 22 returns TRUE for days with temperature above 22, so temps[temps > 22] selects those days.Final Answer:
temps[temps > 22] -> Option CQuick Check:
Use logical condition inside brackets to filter named vectors [OK]
- Using incorrect comparison operators
- Trying to index by string '>22' which is invalid
- Selecting equal or less than values instead of greater
Ready to prove it?
Pass the test and this pattern moves unknown
