Filtering rows in R Programming - Time & Space Complexity
When we filter rows in a data table, we want to know how the time to do this changes as the table gets bigger.
We ask: How does the work grow when there are more rows to check?
Analyze the time complexity of the following code snippet.
# Filter rows where value is greater than 10
filtered_data <- data[data$value > 10, ]
This code checks each row in the data frame and keeps only those with a value greater than 10.
- Primary operation: Checking each row's value to see if it is greater than 10.
- How many times: Once for every row in the data frame.
As the number of rows grows, the number of checks grows the same way.
| 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; doubling rows doubles the work.
Time Complexity: O(n)
This means the time to filter grows in a straight line with the number of rows.
[X] Wrong: "Filtering only a few rows means the operation is very fast no matter the data size."
[OK] Correct: Even if few rows match, the code still checks every row once, so time depends on total rows.
Understanding how filtering scales helps you write efficient data code and explain your choices clearly.
"What if we used a pre-sorted data frame and stopped checking once values were too small? How would the time complexity change?"
Practice
subset() function do in R when working with data frames?Solution
Step 1: Understand the purpose of
Thesubset()and compare with other optionssubset()function is used to pick rows from a data frame that meet a specific condition. Changing column names, adding columns, or sorting are done by other functions, notsubset().Final Answer:
It selects rows based on a condition. -> Option DQuick Check:
subset()filters rows [OK]
- Thinking subset() changes columns
- Confusing subset() with sorting functions
- Using subset() to add data
age is greater than 30 in a data frame df?Solution
Step 1: Check the condition syntax and validate the subset syntax
The correct comparison operator for 'greater than' is >, soage > 30is correct.subset(df, age > 30)correctly filters rows where age is greater than 30.Final Answer:
subset(df, age > 30) -> Option AQuick Check:
Use > for greater than in subset() [OK]
- Using => instead of >
- Using = instead of == or >
- Filtering with wrong comparison signs
df <- data.frame(name=c('Anna','Bob','Cara'), age=c(25, 35, 30)), what is the output of subset(df, age >= 30)?Solution
Step 1: Identify rows where age >= 30 and check which are selected
Bob has age 35 and Cara has age 30, both satisfy age >= 30. Anna has age 25, which does not meet the condition, so only Bob and Cara rows remain.Final Answer:
Rows with Bob and Cara -> Option AQuick Check:
Filter keeps rows with age 30 or more [OK]
- Including rows with age less than 30
- Confusing >= with >
- Assuming all rows are returned
score is less than 50 in data frame df? subset(df, score < 50 &)
Solution
Step 1: Analyze the condition syntax and check other options
The condition ends with '&' which expects another condition after it, but none is provided. Subset can filter numeric columns, '<' is valid, and data frame name is present as 'df'.Final Answer:
The condition ends with an incomplete operator '&'. -> Option BQuick Check:
Conditions must be complete without dangling operators [OK]
- Leaving dangling & or | operators
- Thinking subset can't filter numbers
- Misusing comparison operators
df with columns age and status. How do you filter rows where age is over 25 and status is 'active'?Solution
Step 1: Use correct logical operator for AND and check comparison operators and syntax
The operator for AND in R is a single ampersand '&', so useage > 25 & status == 'active'. Use '==' to compare values, not '=', and combine conditions inside one subset call.Final Answer:
subset(df, age > 25 & status == 'active') -> Option CQuick Check:
Use & and == for combined conditions [OK]
- Using | instead of & for AND
- Using = instead of == for comparison
- Separating conditions with commas
Ready to prove it?
Pass the test and this pattern moves unknown
