Logical indexing in R Programming - Time & Space Complexity
Logical indexing lets us pick elements from a list or vector using true or false values.
We want to see how the time to do this grows as the list gets bigger.
Analyze the time complexity of the following code snippet.
x <- 1:1000
idx <- x %% 2 == 0
result <- x[idx]
This code creates a vector from 1 to 1000, makes a logical vector marking even numbers, and selects those even numbers.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Checking each element to see if it meets the condition (even number).
- How many times: Once for every element in the vector (n times).
As the vector gets bigger, the program checks more elements 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 size of the input.
Time Complexity: O(n)
This means the time to select elements grows in a straight line as the list gets longer.
[X] Wrong: "Logical indexing is instant no matter how big the vector is."
[OK] Correct: The program must check each element to decide if it matches, so bigger vectors take more time.
Understanding how logical indexing scales helps you write efficient data filters and shows you can think about code speed clearly.
"What if we used multiple conditions combined with & or | for logical indexing? How would the time complexity change?"
Practice
v[c(TRUE, FALSE, TRUE)]?Solution
Step 1: Understand logical indexing
Logical indexing uses a TRUE/FALSE vector to pick elements from the original vector where TRUE is present.Step 2: Apply logical vector to
Elements at positions with TRUE in the logical vector are selected; others are skipped.vFinal Answer:
Selects elements ofvwhere the index is TRUE -> Option AQuick Check:
TRUE picks elements [OK]
- Thinking FALSE picks elements
- Confusing logical indexing with numeric indexing
- Assuming logical vector length doesn't matter
x where values are greater than 5 using logical indexing?Solution
Step 1: Understand logical condition syntax
The conditionx > 5creates a logical vector TRUE where elements are greater than 5.Step 2: Use logical vector to index
Usingxx[x > 5]selects elements ofxwhere the condition is TRUE.Final Answer:
x[x > 5] -> Option CQuick Check:
Correct logical indexing syntax [OK]
- Placing condition outside brackets
- Using invalid syntax like
x[> 5] - Swapping operands in condition
v <- c(10, 3, 7, 2, 9) v[v >= 7]
Solution
Step 1: Evaluate the logical condition
This creates a logical vector: TRUE, FALSE, TRUE, FALSE, TRUE for elements 10, 3, 7, 2, 9 respectively.v >= 7Step 2: Select elements where condition is TRUE
Elements at TRUE positions are 10, 7, and 9.Final Answer:
[1] 10 7 9 -> Option DQuick Check:
Only values >= 7 selected [OK]
- Including elements less than 7
- Confusing >= with >
- Expecting an error due to indexing
nums <- c(4, 8, 15, 16, 23, 42) nums[nums > 10 & TRUE]
Solution
Step 1: Analyze logical condition
The expressionnums > 10 & TRUEnums > 10returns a logical vector; AND with TRUE keeps it unchanged.Step 2: Check for errors
The logical vector length matchesnums, and syntax is correct, so no error occurs.Final Answer:
No error, code runs correctly -> Option AQuick Check:
Logical AND with TRUE keeps vector same [OK]
- Thinking & TRUE causes error
- Expecting length mismatch error
- Missing that & works element-wise
temps <- c(23, NA, 19, 25, NA, 21), how can you use logical indexing to select only the non-missing values?Solution
Step 1: Understand how to detect missing values
The functionis.na(temps)returns TRUE for missing values (NA) and FALSE otherwise.Step 2: Use logical NOT to select non-missing
Using!is.na(temps)creates a logical vector TRUE where values are not missing.Step 3: Index
This selects only the non-NA values fromtempswith this logical vectortemps.Final Answer:
temps[!is.na(temps)] -> Option BQuick Check:
Use !is.na() to filter out NA [OK]
- Using temps != NA which is invalid
- Selecting is.na(temps) to get missing values
- Trying to compare with NA using > or <
Ready to prove it?
Pass the test and this pattern moves unknown
