Logical operators (&, |, !, &&, ||) in R Programming - Time & Space Complexity
Logical operators are used to combine or invert true/false values in R. Understanding their time complexity helps us see how fast these checks run as we handle more data.
We want to know how the time to evaluate these operators changes when we use them on vectors of different sizes.
Analyze the time complexity of the following code snippet.
# Logical AND on two vectors
x <- c(TRUE, FALSE, TRUE, FALSE)
y <- c(FALSE, FALSE, TRUE, TRUE)
result <- x & y
# Logical OR on two vectors
result_or <- x | y
# Logical NOT on a vector
result_not <- !x
This code performs element-wise logical AND, OR, and NOT operations on vectors of boolean values.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Element-wise logical operations on vectors.
- How many times: Once per element in the vectors.
Each logical operator checks every element in the input vectors one by one.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The number of operations grows directly with the number of elements.
Time Complexity: O(n)
This means the time to complete the logical operations grows in a straight line with the size of the input vectors.
[X] Wrong: "Logical operators run instantly no matter how big the vectors are."
[OK] Correct: Each element must be checked, so bigger vectors take more time, not zero time.
Knowing how logical operations scale helps you write efficient code when working with large data sets, a skill valued in many programming tasks.
"What if we used the short-circuit operators (&&, ||) on vectors instead of element-wise operators? How would the time complexity change?"
Practice
Solution
Step 1: Understand vectorized logical operators
In R,&and|operate element-wise on vectors, returning a vector of TRUE/FALSE values for each element.Step 2: Compare with single-element operators
&&and||only check the first element of the vectors and return a single TRUE/FALSE value.Final Answer:
& and | -> Option BQuick Check:
Element-wise operators = & and | [OK]
- Confusing && and & as the same
- Using || for vector comparisons
- Thinking ! works on multiple elements like &
v in R?Solution
Step 1: Identify the negation operator in R
The logical NOT operator in R is!, which negates each element of a logical vector.Step 2: Check other options
not()is not a base R function,~is a formula operator, andv != TRUEworks but is less direct and less clear.Final Answer:
!v -> Option AQuick Check:
Negate vector with ! [OK]
- Using not() which is not a base R function
- Confusing ~ with negation
- Using != TRUE instead of !
v1 <- c(TRUE, FALSE, TRUE) v2 <- c(FALSE, FALSE, TRUE) v1 & v2 v1 && v2
Solution
Step 1: Evaluate element-wise AND with &
v1 & v2compares each element:
TRUE & FALSE = FALSE
FALSE & FALSE = FALSE
TRUE & TRUE = TRUE
Result: [FALSE FALSE TRUE]Step 2: Evaluate single-element AND with &&
v1 && v2checks only first elements:
TRUE && FALSE = FALSE
Result: [FALSE]Final Answer:
[1] FALSE FALSE TRUE [1] FALSE -> Option DQuick Check:
& compares all elements, && only first [OK]
- Thinking && works element-wise
- Mixing up & and && results
- Ignoring vector length in &&
v <- c(TRUE, FALSE, TRUE)
if (v & TRUE) {
print("Yes")
} else {
print("No")
}Solution
Step 1: Understand if condition requirements
Theifstatement expects a single TRUE or FALSE value, not a vector.Step 2: Analyze
v & TRUEv & TRUEreturns a logical vector of length 3, which causes an error inifbecause it cannot handle multiple values.Final Answer:
Using & inside if condition causes error due to vector length -> Option AQuick Check:
if needs single TRUE/FALSE, & returns vector [OK]
- Using & instead of && in if
- Assuming if accepts vector
- Ignoring error message about length
a <- c(TRUE, FALSE, TRUE, FALSE) b <- c(FALSE, TRUE, TRUE, TRUE)
Write an expression using logical operators to find elements where either
a is TRUE and b is FALSE, or a is FALSE and b is TRUE. What is the resulting vector?Solution
Step 1: Understand the condition
We want TRUE where exactly one ofaorbis TRUE, but not both. This is the XOR condition.Step 2: Write expression and evaluate
(a & !b) | (!a & b)checks whereais TRUE andbis FALSE, or vice versa.
Evaluate:
a: TRUE FALSE TRUE FALSE
b: FALSE TRUE TRUE TRUE
a & !b: TRUE FALSE FALSE FALSE
!a & b: FALSE TRUE FALSE TRUE
OR: TRUE TRUE FALSE TRUEFinal Answer:
(a & !b) | (!a & b) resulting in [TRUE TRUE FALSE TRUE] -> Option CQuick Check:
XOR = (a & !b) | (!a & b) [OK]
- Using & or | alone instead of XOR logic
- Forgetting to negate one vector
- Confusing XOR with OR
Ready to prove it?
Pass the test and this pattern moves unknown
