Inline R code in R Programming - Time & Space Complexity
We want to see how fast inline R code runs as the input size changes.
How does the time needed grow when we run code inside other code?
Analyze the time complexity of the following code snippet.
x <- 1:1000
result <- sapply(x, function(i) i * 2)
This code doubles each number in a list from 1 to 1000 using inline code inside sapply.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The function doubling each number runs once per element.
- How many times: It runs exactly as many times as the length of x (1000 times here).
Each number is processed one time, so the work grows directly with the input size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 |
| 100 | 100 |
| 1000 | 1000 |
Pattern observation: The time grows in a straight line as input size grows.
Time Complexity: O(n)
This means the time needed grows directly in proportion to how many items we process.
[X] Wrong: "Inline code runs instantly no matter how big the input is."
[OK] Correct: Even inline code runs once per item, so more items mean more work and more time.
Understanding how inline code scales helps you explain how your code handles bigger data smoothly.
"What if we replaced sapply with a vectorized operation? How would the time complexity change?"
Practice
`r expression` in documents?Solution
Step 1: Understand inline R code usage
Inline R code uses backticks andrto embed R expressions inside text.Step 2: Identify the purpose
This allows the result of the R expression to appear directly in the document text, making it dynamic.Final Answer:
To insert the result of an R expression directly into the text -> Option BQuick Check:
Inline R code = insert R result in text [OK]
- Thinking inline code defines variables
- Confusing inline code with comments
- Believing inline code creates functions
Solution
Step 1: Inline R code evaluates expressions
Inline R code executes the R expression between the backticks and inserts its result (here, 2+3=5).Step 2: Check each option's validity
`r sum 2, 3`missing parens around args: sum 2 -> sum(2)=2, then ,3 -> 3 (wrong).`r sum(2 + 3`missing ) -> syntax error.`r 2 + 3`-> 5 (correct).`r sum(2,3`missing ) -> syntax error.Final Answer:
`r 2 + 3` -> Option AQuick Check:
Simple expression inside `r` is best [OK]
- Adding extra parentheses inside sum()
- Using incorrect commas or spaces
- Confusing function call syntax
`r paste('Hello', 'World')`Solution
Step 1: Understand paste() function
The paste() function joins strings with a space by default, so paste('Hello', 'World') returns "Hello World".Step 2: Inline code outputs the result
The inline R code evaluates the expression and inserts the result "Hello World" into the document.Final Answer:
Hello World -> Option DQuick Check:
paste() joins strings with space [OK]
- Expecting no space between words
- Thinking inline code shows code, not result
- Confusing paste() with paste0()
`r mean(c(1, 2, '3'))`Solution
Step 1: Check vector contents
The vector c(1, 2, '3') mixes numbers and a character, so R converts all to character.Step 2: mean() expects numeric input
mean() cannot calculate mean of characters, so it will produce an error.Final Answer:
Character '3' causes mean() to fail -> Option CQuick Check:
mean() needs numeric vector [OK]
- Ignoring type conversion in vectors
- Assuming mean() works on mixed types
- Missing quotes around characters
df inline in your report. Which inline R code correctly shows this count?Solution
Step 1: Recall function to get row count
The functionnrow()returns the number of rows in a data frame.Step 2: Check each option
`r nrow(df)` usesnrow(df)which is correct. `r length(df)` uses length(df) which returns number of columns, not rows. `r df$nrow` tries to access a non-existent columnnrow. `r count(df)` uses count() which is not a base R function for this purpose.Final Answer:
`r nrow(df)` -> Option AQuick Check:
nrow() gives row count [OK]
- Using length() for rows instead of columns
- Trying to access nrow as a column
- Using non-base functions without loading packages
Ready to prove it?
Pass the test and this pattern moves unknown
