Code chunks and output in R Programming - Time & Space Complexity
We want to see how the time to run code chunks grows as we add more lines or commands.
How does the number of code chunks affect the total work done?
Analyze the time complexity of the following code snippet.
# Print numbers from 1 to n
print_numbers <- function(n) {
for (i in 1:n) {
print(i)
}
}
print_numbers(5)
This code prints numbers from 1 up to n, one by one.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that prints each number.
- How many times: Exactly n times, once for each number.
As n grows, the number of print operations grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 prints |
| 100 | 100 prints |
| 1000 | 1000 prints |
Pattern observation: The work grows directly with n, so doubling n doubles the work.
Time Complexity: O(n)
This means the time to run grows in a straight line with the number of items printed.
[X] Wrong: "The code runs instantly no matter how big n is."
[OK] Correct: Each number must be printed one by one, so more numbers mean more time.
Understanding how loops affect time helps you explain code efficiency clearly and confidently.
"What if we changed the loop to print only every second number? How would the time complexity change?"
Practice
Solution
Step 1: Understand R Markdown structure
R Markdown uses code chunks to embed R code inside text documents.Step 2: Purpose of code chunks
Code chunks run R code and display results inline, mixing code and output.Final Answer:
To run R code and show its output inline with text -> Option AQuick Check:
Code chunk = run code + show output [OK]
- Thinking code chunks only hold text
- Confusing code chunks with document sections
- Assuming code chunks add images automatically
Solution
Step 1: Recall R Markdown chunk syntax
R code chunks start with three backticks followed by {r} exactly.Step 2: Check each option
Only ```{r} correctly starts a chunk; others are invalid syntax.Final Answer:
```{r} -> Option CQuick Check:
Chunk start = ```{r} [OK]
- Omitting curly braces around r
- Using only two backticks
- Confusing HTML tags with chunk syntax
```{r}
x <- 5
x * 2
```Solution
Step 1: Understand the code execution
Variable x is assigned 5, then multiplied by 2, resulting in 10.Step 2: R output format
R prints numeric output with index [1] before the value.Final Answer:
[1] 10 -> Option AQuick Check:
5 * 2 = 10 with [1] prefix [OK]
- Ignoring the [1] prefix in output
- Expecting just '10' without index
- Thinking code prints the expression itself
```{r}
print("Hello)
```Solution
Step 1: Check the string syntax
The string starts with a double quote but lacks the closing double quote.Step 2: Consequence of missing quote
This causes a syntax error because the string is not properly closed.Final Answer:
Missing closing quote in the string -> Option BQuick Check:
Unclosed string causes syntax error [OK]
- Assuming chunk braces cause error
- Thinking print is misspelled
- Ignoring missing quote errors
Solution
Step 1: Understand chunk options
echo=FALSE hides the code but runs it, showing output only.Step 2: Compare other options
eval=FALSE skips running code, include=FALSE hides both code and output, message=FALSE hides messages only.Final Answer:
```{r, echo=FALSE} -> Option DQuick Check:
echo=FALSE hides code, shows output [OK]
- Using eval=FALSE hides output too
- Using include=FALSE hides everything
- Confusing message=FALSE with code hiding
Ready to prove it?
Pass the test and this pattern moves unknown
