Running R code in R Programming - Time & Space Complexity
When we run R code, we want to know how long it takes as the input grows.
We ask: How does the work increase when we run more or bigger data?
Analyze the time complexity of the following code snippet.
run_r_code <- function(n) {
result <- numeric(n)
for (i in 1:n) {
result[i] <- i * 2
}
return(result)
}
This code runs a loop from 1 to n and doubles each number, storing it in a result vector.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that runs from 1 to n.
- How many times: Exactly n times, once for each number.
As n grows, the loop runs more times, so the work grows directly with n.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 operations |
| 100 | 100 operations |
| 1000 | 1000 operations |
Pattern observation: Doubling the input doubles the work needed.
Time Complexity: O(n)
This means the time to run grows in a straight line with the input size.
[X] Wrong: "The loop runs faster because doubling a number is simple."
[OK] Correct: Even if the operation is simple, the loop still runs n times, so time grows with n.
Understanding how loops affect time helps you explain code efficiency clearly and confidently.
"What if we replaced the for-loop with a vectorized operation? How would the time complexity change?"
Practice
Solution
Step 1: Understand the meaning of running code
Running code means executing commands or scripts to perform actions in R.Step 2: Identify the correct description
Typing commands or running script files matches the definition of running R code.Final Answer:
Typing commands or running script files to perform tasks -> Option DQuick Check:
Running code = executing commands/scripts [OK]
- Confusing running code with just writing comments
- Thinking saving files runs code
- Mixing running code with installing software
script.R from the console?Solution
Step 1: Recall the function to run scripts in R
The functionsource()is used to run R scripts from the console.Step 2: Match the correct syntax
source('script.R')correctly runs the script namedscript.R.Final Answer:
source('script.R') -> Option AQuick Check:
Run script = source() [OK]
- Using run() which is not an R function
- Using execute() which does not exist in R
- Using load() which loads data, not scripts
print(2 + 3 * 4)
Solution
Step 1: Understand operator precedence in R
Multiplication (*) has higher precedence than addition (+), so 3 * 4 is calculated first.Step 2: Calculate the expression
3 * 4 = 12, then 2 + 12 = 14.Final Answer:
14 -> Option AQuick Check:
2 + (3 * 4) = 14 [OK]
- Adding before multiplying ignoring precedence
- Expecting error due to syntax
- Confusing multiplication with addition
print('Hello World)Solution
Step 1: Check string syntax
The string starts with a single quote but does not have a matching closing quote.Step 2: Confirm the error type
This causes a syntax error because the string is not properly closed.Final Answer:
Missing closing quote for the string -> Option BQuick Check:
Unclosed string = syntax error [OK]
- Thinking print() is invalid in R
- Ignoring missing quote and blaming parentheses
- Assuming code runs without errors
analysis.R. Which is the best way to run all commands at once and see their output in the console?Solution
Step 1: Understand how to run multiple commands in R
Running a whole script at once is done using thesource()function.Step 2: Identify the best method for running all commands and seeing output
source('analysis.R')runs all commands in the file and shows output in the console.Final Answer:
Use source('analysis.R') to run the whole script -> Option CQuick Check:
Run script = source() [OK]
- Running lines manually wastes time
- Copy-pasting line by line is inefficient
- Restarting R does not run code automatically
Ready to prove it?
Pass the test and this pattern moves unknown
