filter() for row selection in R Programming - Time & Space Complexity
When we use filter() to select rows in R, it checks each row to see if it meets the condition.
We want to know how the time it takes grows as the number of rows grows.
Analyze the time complexity of the following code snippet.
library(dplyr)
data <- data.frame(
id = 1:1000,
score = sample(1:100, 1000, replace = TRUE)
)
filtered_data <- filter(data, score > 50)
This code creates a data frame with 1000 rows and selects rows where the score is greater than 50.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Checking each row's score against the condition
score > 50. - How many times: Once for every row in the data frame.
As the number of rows increases, the number of checks grows at the same rate.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The work grows directly with the number of rows.
Time Complexity: O(n)
This means the time to filter rows grows in a straight line as the number of rows grows.
[X] Wrong: "Filtering rows is instant no matter how many rows there are."
[OK] Correct: Each row must be checked, so more rows mean more work and more time.
Understanding how filtering scales helps you write efficient data code and explain your choices clearly.
"What if we filter using multiple conditions combined with AND or OR? How would the time complexity change?"
Practice
filter() function in R do when applied to a data frame?Solution
Step 1: Understand the purpose of
Thefilter()filter()function is used to pick rows from a data frame that satisfy certain conditions.Step 2: Compare with other functions
Functions likeselect()pick columns, andarrange()sorts rows, so they do not matchfilter()'s role.Final Answer:
Selects rows that meet specific conditions -> Option AQuick Check:
filter()= row selection [OK]
- Confusing filter() with select()
- Thinking filter() sorts data
- Assuming filter() adds columns
age is greater than 30 using dplyr?Solution
Step 1: Check the comparison operator
The correct operator for 'greater than' is >, soage > 30is valid.Step 2: Validate syntax correctness
filter(data, age > 30)correctly applies the condition insidefilter(). Other options use wrong operators or conditions.Final Answer:
filter(data, age > 30) -> Option BQuick Check:
Use > for greater than in filter() [OK]
- Using => instead of >
- Using = instead of == or >
- Confusing < with >
df below:df <- data.frame(name = c('Anna', 'Bob', 'Cara'), age = c(25, 35, 30))What will be the output of
filter(df, age >= 30)?Solution
Step 1: Identify rows where age >= 30
From the data, Bob has age 35 and Cara has age 30, both satisfy age >= 30.Step 2: Confirm which rows are selected
Anna has age 25, which is less than 30, so she is excluded.Final Answer:
Rows with Bob and Cara -> Option DQuick Check:
age >= 30 selects Bob and Cara [OK]
- Including rows with age less than 30
- Misreading >= as >
- Confusing row names with values
library(dplyr) df <- data.frame(score = c(80, 90, 70)) filtered <- filter(df, score => 80)
What is the problem with this code?
Solution
Step 1: Check the operator used in filter()
The operator => is not valid in R; the correct operator for 'greater or equal' is >=.Step 2: Validate other parts of the code
The data frame has the correct column, filter() can be used without pipe, and column names do not need quotes inside filter().Final Answer:
The operator => is invalid in R -> Option AQuick Check:
Use >=, not =>, for comparisons [OK]
- Using => instead of >=
- Thinking filter() always needs %>%
- Quoting column names inside filter()
df with columns city and temperature. You want to select rows where city is either 'Paris' or 'London' and temperature is above 20. Which filter() command correctly does this?Solution
Step 1: Understand operator precedence in R
Logical AND (&) has higher precedence than OR (|), so parentheses are needed to group city conditions.Step 2: Analyze each option's logic
filter(df, (city == 'Paris' | city == 'London') & temperature > 20) correctly groups city conditions with parentheses and then ANDs with temperature > 20. filter(df, city == 'Paris' | city == 'London' & temperature > 20) lacks parentheses, causing wrong logic. filter(df, city == 'Paris' & city == 'London' & temperature > 20) requires city to be both Paris and London simultaneously, impossible. filter(df, city == 'Paris' || city == 'London' && temperature > 20) uses || and && which are not vectorized and cause errors in filter().Final Answer:
filter(df, (city == 'Paris' | city == 'London') & temperature > 20) -> Option CQuick Check:
Use parentheses to group OR conditions in filter() [OK]
- Missing parentheses causing wrong logic
- Using && and || instead of & and |
- Trying city == 'Paris' & city == 'London' together
Ready to prove it?
Pass the test and this pattern moves unknown
