Numeric and character vectors in R Programming - Time & Space Complexity
When working with numeric and character vectors in R, it is important to understand how the time to process them grows as their size increases.
We want to know how the number of steps changes when we do operations on these vectors.
Analyze the time complexity of the following code snippet.
# Create numeric vector
num_vec <- 1:1000
# Create character vector
char_vec <- as.character(num_vec)
# Convert numeric vector to character vector
result <- as.character(num_vec)
# Concatenate two character vectors
combined <- c(char_vec, result)
This code creates numeric and character vectors, converts numeric to character, and combines two character vectors.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Converting each element of the numeric vector to a character string.
- How many times: Once for each element in the vector (n times).
- Secondary operation: Concatenating two character vectors, which involves copying elements.
- How many times: Proportional to the total length of both vectors (about 2n elements copied).
As the vector size grows, the number of operations grows roughly in direct proportion.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 conversions + 20 concatenations |
| 100 | About 100 conversions + 200 concatenations |
| 1000 | About 1000 conversions + 2000 concatenations |
Pattern observation: The total steps increase roughly in a straight line as input size grows.
Time Complexity: O(n)
This means the time to run the code grows directly with the size of the vectors.
[X] Wrong: "Converting or combining vectors takes the same time no matter how big they are."
[OK] Correct: Each element must be processed, so bigger vectors take more time.
Understanding how vector operations scale helps you write efficient R code and explain your reasoning clearly in interviews.
"What if we used a list instead of a vector? How would the time complexity change?"
Practice
Solution
Step 1: Understand numeric vector creation
Numeric vectors contain only numbers, so elements must be numeric values.Step 2: Check each option's elements
c(1, 2, 3, 4)has only numbers, so it is a numeric vector.c("one", "two", "three")has text,c(TRUE, FALSE, TRUE)has logical values, andc(1, "two", 3)mixes numbers and text, which converts all to characters.Final Answer:
c(1, 2, 3, 4) -> Option DQuick Check:
Numeric vector = c(1, 2, 3, 4) [OK]
- Mixing numbers and text creates character vector
- Confusing logical vectors with numeric
- Using quotes around numbers
Solution
Step 1: Identify character vector syntax
Character vectors contain text strings, which must be inside quotes.Step 2: Check options for quoted text
c("apple", "banana", "cherry")has all elements as quoted text strings, so it is a character vector.c(1, 2, 3)is numeric,c(TRUE, FALSE, TRUE)is logical, andc(1, "banana", 3)mixes types causing coercion.Final Answer:
c("apple", "banana", "cherry") -> Option BQuick Check:
Character vector = c("apple", "banana", "cherry") [OK]
- Forgetting quotes around text
- Mixing types in one vector
- Using numbers without quotes for text
x <- c(5, 10, 15)
y <- c("a", "b", "c")
z <- c(x, y)
print(z)
Solution
Step 1: Understand vector combining rules in R
When combining numeric and character vectors, R converts all elements to character to keep the same type.Step 2: Analyze the combined vector z
Vector z contains numbers 5, 10, 15 and characters "a", "b", "c". All numbers become strings, so print shows all as character strings.Final Answer:
[1] "5" "10" "15" "a" "b" "c" -> Option AQuick Check:
Mixed vector coerces to character = [1] "5" "10" "15" "a" "b" "c" [OK]
- Expecting numeric values to stay numeric
- Thinking R throws error on combining types
- Assuming NA values appear for mismatched types
vec <- c(1, 2, "three", 4)
sum(vec)
Solution
Step 1: Understand vector creation with mixed types
c(1, 2, "three", 4) creates a character vector because of the text element "three".Step 2: Check sum() behavior on character vectors
sum() expects numeric input. Passing a character vector causes an error because it cannot sum text.Final Answer:
Error because sum() cannot handle character vectors -> Option CQuick Check:
sum(character vector) = Error [OK]
- Assuming sum() converts characters to numbers
- Thinking c() forbids mixing types (it coerces instead)
- Expecting sum() to return NA silently
nums <- c(2, 4, 6, 8)
chars <- c("2", "4", "6", "8")
How can you create a numeric vector from
chars and add it to nums element-wise?Solution
Step 1: Convert character vector to numeric
Use as.numeric(chars) to change text numbers to numeric values.Step 2: Add numeric vectors element-wise
Adding nums + as.numeric(chars) adds corresponding elements correctly.Final Answer:
nums + as.numeric(chars) -> Option AQuick Check:
Convert chars to numeric before adding = nums + as.numeric(chars) [OK]
- Adding numeric and character vectors directly
- Using sum() instead of element-wise addition
- Converting numeric to character instead of chars to numeric
Ready to prove it?
Pass the test and this pattern moves unknown
