It selects rows from a data frame that meet a specific condition, like picking only the apples from a basket of fruits.
Click to reveal answer
beginner
How do you use filter() to select rows where a column age is greater than 30?
Use filter(data, age > 30). This keeps only rows where the age is more than 30.
Click to reveal answer
intermediate
Can filter() handle multiple conditions? How?
Yes! Use commas or logical operators like & or |. For example, filter(data, age > 30, gender == 'F') selects rows where age is over 30 AND gender is female.
Click to reveal answer
beginner
What package do you need to use filter() for data frames?
The dplyr package. It makes data manipulation easy and clear.
Click to reveal answer
beginner
What happens if no rows meet the condition in filter()?
You get an empty data frame with the same columns but zero rows, like an empty basket after picking fruits.
Click to reveal answer
Which function is used to select rows based on conditions in R's dplyr package?
Amutate()
Bselect()
Carrange()
Dfilter()
✗ Incorrect
filter() selects rows by condition. select() chooses columns, arrange() sorts rows, and mutate() adds or changes columns.
What will filter(data, age > 25, gender == 'M') do?
ASelect rows where age is over 25 and gender is male
BSelect rows where age is over 25 or gender is male
CSelect rows where age is under 25 and gender is male
DSelect all rows
✗ Incorrect
Using commas means AND condition, so both must be true.
If no rows match the filter condition, what does filter() return?
AAn empty data frame with zero rows
BThe original data frame
CAn error
DNULL
✗ Incorrect
filter() returns an empty data frame with the same columns but no rows.
Which package must be loaded to use filter() for data frames?
Aggplot2
Bdplyr
Ctidyr
Dbase
✗ Incorrect
dplyr provides filter() for easy row selection.
How do you combine multiple conditions inside filter()?
AUsing parentheses only
BUsing plus signs (+)
CUsing commas or logical operators like && and |
DUsing semicolons (;)
✗ Incorrect
Commas act like AND, and logical operators combine conditions logically.
Explain how to use filter() to select rows from a data frame based on one or more conditions.
Think about how you pick only certain fruits from a basket based on color or size.
You got /5 concepts.
What happens when filter() finds no rows matching the condition? How can this be useful?
Imagine looking for red apples but finding none.
You got /5 concepts.
Practice
(1/5)
1. What does the filter() function in R do when applied to a data frame?
easy
A. Selects rows that meet specific conditions
B. Selects columns based on their names
C. Sorts the data frame by a column
D. Adds new columns to the data frame
Solution
Step 1: Understand the purpose of filter()
The filter() function is used to pick rows from a data frame that satisfy certain conditions.
Step 2: Compare with other functions
Functions like select() pick columns, and arrange() sorts rows, so they do not match filter()'s role.
Final Answer:
Selects rows that meet specific conditions -> Option A
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 A
Quick Check:
Use >=, not =>, for comparisons [OK]
Hint: Use >= for 'greater or equal' in R, not => [OK]
Common Mistakes:
Using => instead of >=
Thinking filter() always needs %>%
Quoting column names inside filter()
5. You have a data frame 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?
hard
A. filter(df, city == 'Paris' | city == 'London' & temperature > 20)
B. filter(df, city == 'Paris' & city == 'London' & temperature > 20)
C. filter(df, (city == 'Paris' | city == 'London') & temperature > 20)
D. filter(df, city == 'Paris' || city == 'London' && temperature > 20)
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 C
Quick Check:
Use parentheses to group OR conditions in filter() [OK]
Hint: Use parentheses to group OR conditions before AND [OK]
Common Mistakes:
Missing parentheses causing wrong logic
Using && and || instead of & and |
Trying city == 'Paris' & city == 'London' together
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.