📖 Scenario: You work in a small bookstore and have a list of books with their prices and stock. You want to find which books cost less than $20 to plan a sale.
🎯 Goal: Build a small R program that creates a data frame of books, sets a price limit, filters the books cheaper than that limit using filter(), and prints the filtered list.
📋 What You'll Learn
Create a data frame called books with columns title, price, and stock with exact values
Create a variable called max_price set to 20
Use dplyr::filter() with books and price < max_price to get cheaper books
Print the filtered data frame
💡 Why This Matters
🌍 Real World
Filtering data frames is a common task in data analysis to focus on relevant information, like finding affordable products or available stock.
💼 Career
Data analysts and scientists often use filtering to clean and prepare data before making reports or decisions.
Progress0 / 4 steps
1
Create the books data frame
Create a data frame called books with these exact entries: title: "Book A", "Book B", "Book C", "Book D" price: 15, 25, 10, 30 stock: 5, 2, 8, 1
R Programming
Hint
Use data.frame() with vectors for each column named title, price, and stock.
2
Set the maximum price limit
Create a variable called max_price and set it to 20
R Programming
Hint
Just assign the number 20 to a variable named max_price.
3
Filter books cheaper than max_price
Use dplyr::filter() with books and the condition price < max_price to create a new data frame called cheap_books
R Programming
Hint
Remember to load dplyr with library(dplyr) before using filter().
4
Print the filtered books
Write a print() statement to display the cheap_books data frame
R Programming
Hint
Use print(cheap_books) to show the filtered books.
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.