R Console and script files in R Programming - Time & Space Complexity
When running R code in the console or script files, it's helpful to know how the time to run your code changes as your data grows.
We want to see how the time needed grows when we run commands repeatedly or on bigger inputs.
Analyze the time complexity of the following code snippet.
# Calculate squares of numbers from 1 to n
n <- 1000
result <- numeric(n)
for (i in 1:n) {
result[i] <- i^2
}
This code calculates the square of each number from 1 up to n and stores it in a 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 number of times the loop runs grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 |
| 100 | 100 |
| 1000 | 1000 |
Pattern observation: The operations increase directly with n; doubling n doubles the work.
Time Complexity: O(n)
This means the time to run the code grows in a straight line with the size of the input.
[X] Wrong: "The loop runs faster because computers are fast, so time complexity doesn't matter here."
[OK] Correct: Even if computers are fast, as n grows very large, the time will still increase linearly, so understanding this helps predict performance.
Knowing how loops in R scripts or console commands scale with input size shows you understand how your code behaves with bigger data, a key skill in programming.
"What if we replaced the for-loop with a vectorized operation like result <- (1:n)^2? How would the time complexity change?"
Practice
Solution
Step 1: Understand the R Console function
The R Console allows users to type commands and see results immediately, one command at a time.Step 2: Compare with script files
Script files are used to save many commands to run later, not the console's main role.Final Answer:
To type and run commands one at a time -> Option AQuick Check:
R Console = run commands one by one [OK]
- Confusing console with script file
- Thinking console saves commands permanently
- Assuming console only creates plots
analysis.R in R?Solution
Step 1: Recall R function to run scripts
In R, the functionsource()is used to run commands saved in a script file.Step 2: Check other options
Functions likerun(),execute(), andload()do not run script files;load()loads saved data, not scripts.Final Answer:
source("analysis.R") -> Option CQuick Check:
Run script = source() [OK]
- Using load() to run scripts
- Trying run() or execute() which don't exist
- Forgetting to use quotes around filename
x <- 5 y <- 3 z <- x + y print(z)
Solution
Step 1: Calculate the sum of x and y
x is assigned 5, y is assigned 3, so z = 5 + 3 = 8.Step 2: Print the value of z
The print(z) command outputs the value stored in z, which is 8.Final Answer:
8 -> Option DQuick Check:
5 + 3 = 8 [OK]
- Concatenating numbers as strings (53)
- Forgetting to assign z before print
- Expecting NULL output
calc.R:result <- a + b print(result)
When you run
source("calc.R") in the console, you get an error. What is the problem?Solution
Step 1: Check variable definitions
The script uses variables a and b without assigning values first, causing an error.Step 2: Verify other parts
print() is correct, source() runs scripts, and <- is valid assignment in R.Final Answer:
Variables a and b are not defined before use -> Option AQuick Check:
Undefined variables cause errors [OK]
- Assuming print() is wrong
- Thinking source() can't run scripts
- Confusing assignment operator syntax
Solution
Step 1: Understand script file usage
Script files save many commands so you can run them together later.Step 2: Use source() to run scripts
Thesource()function runs all commands in the script file at once.Step 3: Evaluate other options
Typing in console is manual and slow; text files alone don't run commands;load()loads data, not scripts.Final Answer:
Write commands in a script file and use source() to run them -> Option BQuick Check:
Scripts + source() = organized runs [OK]
- Running scripts by typing manually
- Using load() instead of source()
- Not saving commands in R script format
Ready to prove it?
Pass the test and this pattern moves unknown
