Comparison operators in R Programming - Time & Space Complexity
We want to understand how long it takes to compare values using comparison operators in R.
How does the time needed change when we compare more items?
Analyze the time complexity of the following code snippet.
values <- c(3, 7, 2, 9, 5)
results <- logical(length(values))
for (i in seq_along(values)) {
results[i] <- values[i] > 4
}
This code checks each number in a list to see if it is greater than 4.
- Primary operation: Comparing each element to the number 4 using > operator.
- How many times: Once for each element in the list.
Each new item adds one more comparison to do.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 comparisons |
| 100 | 100 comparisons |
| 1000 | 1000 comparisons |
Pattern observation: The number of comparisons grows directly with the number of items.
Time Complexity: O(n)
This means the time to compare grows in a straight line as the list gets bigger.
[X] Wrong: "Comparing many items is instant and does not depend on list size."
[OK] Correct: Each item needs its own comparison, so more items mean more time.
Understanding how comparison operations scale helps you write efficient code and explain your thinking clearly.
"What if we compared every item to every other item? How would the time complexity change?"
Practice
Solution
Step 1: Understand equality operator in R
In R, the operator to check if two values are equal is==.Step 2: Differentiate from other operators
=is for assignment,!=means not equal, and<>is not valid in R.Final Answer:
== -> Option DQuick Check:
Equality operator = == [OK]
- Using single = instead of ==
- Confusing != with equality
- Using <> which is invalid in R
Solution
Step 1: Identify the not equal operator in R
R uses!=to check if two values are not equal.Step 2: Check syntax correctness
<>and!==are not valid in R, and=!is incorrect syntax.Final Answer:
x != 10 -> Option AQuick Check:
Not equal operator = != [OK]
- Using <> which is invalid in R
- Writing =! instead of !=
- Using !== which is from other languages
x <- 5
y <- 7
x > y
Solution
Step 1: Compare values of x and y
x is 5 and y is 7, so 5 > 7 is being checked.Step 2: Evaluate the comparison
Since 5 is not greater than 7, the expression returns FALSE.Final Answer:
FALSE -> Option AQuick Check:
5 > 7 = FALSE [OK]
- Confusing > with <
- Expecting numeric output instead of logical
- Thinking it causes an error
if (x = 10) { print("Yes") }Solution
Step 1: Check the condition syntax in if statement
In R, to compare values, use==, not=.Step 2: Understand the difference between assignment and comparison
=assigns a value, but==tests equality. Using=in if condition causes an error or unexpected behavior.Final Answer:
Using = instead of == -> Option CQuick Check:
Comparison needs ==, not = [OK]
- Using = instead of ==
- Ignoring syntax errors from wrong operator
- Thinking = works for comparison
v <- c(3, 5, 7, 9) to keep only values greater than or equal to 5. Which R expression correctly uses comparison operators to do this?Solution
Step 1: Understand filtering with comparison
To keep values >= 5, use the operator >= inside the indexing brackets.Step 2: Check syntax correctness
=>,=, and>==are invalid operators in R.Final Answer:
v[v >= 5] -> Option BQuick Check:
Use >= for greater or equal in filtering [OK]
- Using => which is invalid
- Using = instead of comparison operator
- Combining > and == incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
