First R program in R Programming - Time & Space Complexity
We want to see how the time it takes to run a simple R program changes as we give it more work.
How does the program's running time grow when the input size grows?
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: It runs exactly n times, once for each number.
As n gets bigger, the program prints more numbers, so it takes more time.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 prints |
| 100 | 100 prints |
| 1000 | 1000 prints |
Pattern observation: The time grows directly with n; doubling n doubles the work.
Time Complexity: O(n)
This means the time to run the program grows in a straight line with the input size.
[X] Wrong: "The program 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 running time is a key skill that helps you write efficient code and explain your thinking clearly.
"What if we changed the loop to print only every second number? How would the time complexity change?"
Practice
print() function do in R?Solution
Step 1: Understand the purpose of print()
Theprint()function is used to show output on the screen in R.Step 2: Compare options
Only It displays the text or value inside the parentheses on the screen. correctly describes thatprint()displays text or values.Final Answer:
It displays the text or value inside the parentheses on the screen. -> Option BQuick Check:
print() shows output = It displays the text or value inside the parentheses on the screen. [OK]
- Thinking print() saves to a file
- Confusing print() with variable creation
- Assuming print() deletes data
Solution
Step 1: Check the use of quotes
Text messages must be inside matching quotes. print("Hello, R!") uses double quotes correctly.Step 2: Verify function syntax
print() requires parentheses, not square brackets. print("Hello, R!") uses parentheses correctly.Final Answer:
print("Hello, R!") -> Option AQuick Check:
Quotes + parentheses correct = print("Hello, R!") [OK]
- Missing quotes around text
- Mismatched or missing closing quotes
- Using square brackets instead of parentheses
print("Welcome to R programming")Solution
Step 1: Understand print() output format in R
R prints output with an index [1] before the text when printing a character string.Step 2: Match output with options
[1] "Welcome to R programming" shows the correct R output format including the index.Final Answer:
[1] "Welcome to R programming" -> Option AQuick Check:
print() output includes [1] index = [1] "Welcome to R programming" [OK]
- Ignoring the [1] index in output
- Expecting plain text without index
- Confusing output with error messages
print('Hello R)Solution
Step 1: Check quotes in the code
The code starts with a single quote but does not close it properly, causing mismatched quotes.Step 2: Verify parentheses
The closing parenthesis is present, so the error is not missing parenthesis.Final Answer:
Mismatched quotes around text -> Option CQuick Check:
Quotes must match exactly = Mismatched quotes around text [OK]
- Ignoring missing quote errors
- Assuming parentheses are missing
- Thinking print() needs commas inside
Solution
Step 1: Understand how to print multiple lines in R
Using two separate print() calls prints on separate lines. The newline character \n inside a single print() prints \n literally, not a new line.Step 2: Check code correctness
print("Name: Alice") print("Age: 25") correctly uses two print() calls on separate lines with proper syntax.Final Answer:
print("Name: Alice") print("Age: 25") -> Option DQuick Check:
Separate print() calls for separate lines = print("Name: Alice") print("Age: 25") [OK]
- Using \n inside print() without escape
- Writing \n outside quotes causing syntax error
- Trying to print multiple lines in one print() without proper escape
Ready to prove it?
Pass the test and this pattern moves unknown
