Numeric type in R Programming - Time & Space Complexity
We want to understand how the time to work with numeric types changes as the size of the numbers or data grows.
How does the time needed to perform numeric operations grow when we handle bigger or more numbers?
Analyze the time complexity of the following code snippet.
# Sum numbers from 1 to n
sum_numbers <- function(n) {
total <- 0
for (i in 1:n) {
total <- total + i
}
return(total)
}
result <- sum_numbers(1000)
print(result)
This code adds all numbers from 1 up to n and returns the total sum.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Adding a number to the total inside a loop.
- How many times: The loop runs n times, once for each number from 1 to n.
As n grows, the number of additions grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 additions |
| 100 | 100 additions |
| 1000 | 1000 additions |
Pattern observation: The number of operations grows directly with n, so doubling n doubles the work.
Time Complexity: O(n)
This means the time to complete the sum grows in a straight line as the input size grows.
[X] Wrong: "Adding numbers from 1 to n takes the same time no matter how big n is."
[OK] Correct: Each number must be added one by one, so more numbers mean more time.
Understanding how loops over numbers affect time helps you explain and improve code that processes data step by step.
"What if we used a built-in function that sums numbers without a loop? How would the time complexity change?"
Practice
Solution
Step 1: Understand numeric and integer types in R
In R, numeric type stores decimal numbers by default, while integer type stores whole numbers with an L suffix.Step 2: Evaluate each option
Numeric type stores decimal numbers by default correctly states that numeric stores decimals by default. Options A, B, and C are incorrect based on R's type system.Final Answer:
Numeric type stores decimal numbers by default. -> Option CQuick Check:
Numeric = decimals [OK]
- Confusing integer and numeric types
- Thinking quotes are needed for numbers
- Assuming numeric and integer are identical
Solution
Step 1: Recall integer creation syntax in R
In R, to create an integer literal, you add L after the number, e.g., 5L.Step 2: Check each option
x <- 5L uses 5L which is correct. x <- 5 creates a numeric (decimal) 5, not integer. x <- "5" creates a string. x <- as.integer("5L") fails to convert the string "5L" to integer due to the L suffix, resulting in NA.Final Answer:
x <- 5L -> Option AQuick Check:
Integer literal = number + L [OK]
- Using plain numbers for integers
- Confusing strings with numbers
- Using as.integer on decimals expecting exact integer
x <- as.numeric("3.14")
typeof(x)Solution
Step 1: Understand as.numeric() conversion
as.numeric() converts a string to a numeric type, which in R is stored as double precision floating point.Step 2: Check typeof() result for numeric
typeof() returns "double" for numeric values with decimals in R.Final Answer:
"double" -> Option AQuick Check:
as.numeric() returns double type [OK]
- Expecting integer type from as.numeric()
- Confusing character and numeric types
- Thinking numeric means integer
y <- 10L
z <- "abc"
sum <- y + z
Solution
Step 1: Check addition of integer and character
z <- "abc" creates a character value. Adding integer y (10L) and character z causes an error because character cannot be converted to numeric for arithmetic operations.Step 2: Confirm the error
The error is 'non-numeric argument to binary operator' since "abc" cannot convert to numeric.Final Answer:
Error because "abc" cannot convert to numeric -> Option DQuick Check:
String in arithmetic causes error [OK]
- Trying to perform arithmetic on strings
- Thinking 10L is not numeric
- Assuming sum is reserved and causes error
c("1", "2.5", "3L", "four"). Which R code correctly converts only the valid numeric values to numeric type, ignoring invalid ones?Solution
Step 1: Understand the problem with "3L" and "four"
"3L" is not directly numeric because of the L suffix as a string; "four" is invalid.Step 2: Use gsub to remove "L" and convert
Removing "L" from strings allows as.numeric() to convert "3" correctly; "four" becomes NA.Step 3: Check options
as.numeric(gsub("L", "", c("1", "2.5", "3L", "four"))) removes "L" before conversion, correctly converting valid numbers and producing NA for "four". Others either do not handle "3L" or convert incorrectly.Final Answer:
as.numeric(gsub("L", "", c("1", "2.5", "3L", "four"))) -> Option BQuick Check:
Remove non-numeric chars before conversion [OK]
- Trying to convert strings with letters directly
- Using as.integer on decimals
- Ignoring warnings from invalid conversions
Ready to prove it?
Pass the test and this pattern moves unknown
