📖 Scenario: You have a small dataset of sales records for a store. You want to find out which products sold more than 50 units and see their total sales. Doing this manually is slow and confusing. Using dplyr in R makes this easy and clear.
🎯 Goal: Build a simple R script that uses dplyr to filter products with sales over 50 units and then select their names and total sales.
📋 What You'll Learn
Create a data frame called sales_data with product names and units sold
Create a threshold variable called min_units set to 50
Use dplyr functions filter() and select() to get products with units sold greater than min_units and only show product names and units sold
Print the resulting filtered data frame
💡 Why This Matters
🌍 Real World
Data analysts often need to quickly filter and select important parts of large datasets. dplyr makes this easy and readable.
💼 Career
Knowing dplyr is essential for data science and analytics jobs that use R, as it speeds up data cleaning and preparation.
Progress0 / 4 steps
1
Create the sales data frame
Create a data frame called sales_data with these exact entries: product column with values 'Apples', 'Bananas', 'Cherries', 'Dates', and 'Elderberries', and units_sold column with values 30, 60, 45, 80, and 20 respectively.
R Programming
Hint
Use data.frame() with two columns named product and units_sold.
2
Set the minimum units threshold
Create a variable called min_units and set it to 50.
R Programming
Hint
Just assign 50 to a variable named min_units.
3
Filter and select with dplyr
Load the dplyr library. Then create a new data frame called filtered_sales by filtering sales_data to keep only rows where units_sold is greater than min_units. Use select() to keep only the product and units_sold columns.
R Programming
Hint
Use library(dplyr) to load the package. Then use the pipe %>% to chain filter() and select().
4
Print the filtered sales data
Print the filtered_sales data frame to show the products with units sold greater than min_units.
R Programming
Hint
Use print(filtered_sales) to show the filtered data.
Practice
(1/5)
1. Why does dplyr make data wrangling easier for beginners?
easy
A. It uses simple verbs like filter and select to describe actions clearly.
B. It requires writing complex loops for data manipulation.
C. It only works with small datasets.
D. It replaces all R functions with new ones.
Solution
Step 1: Understand dplyr verbs
dplyr uses easy-to-understand verbs like filter and select to describe what you want to do with data.
Step 2: Compare with other methods
Unlike writing loops or complex code, these verbs make code simpler and clearer for beginners.
Final Answer:
It uses simple verbs like filter and select to describe actions clearly. -> Option A
2. Which of the following is the correct way to use the pipe operator %>% in dplyr?
easy
A. data %>% filter(age > 30) select(name, age)
B. data |> filter(age > 30) |> select(name, age)
C. filter(data, age > 30) %>% select(name, age)
D. data %>% filter(age > 30) %>% select(name, age)
Solution
Step 1: Identify correct pipe usage
The pipe %>% passes the data from one function to the next, so chaining filter then select is done as shown in data %>% filter(age > 30) %>% select(name, age).
Step 2: Check other options for errors
data |> filter(age > 30) |> select(name, age) uses a different pipe not standard in dplyr, filter(data, age > 30) %>% select(name, age) misplaces the pipe, and data %>% filter(age > 30) select(name, age) misses the pipe before select.
Final Answer:
data %>% filter(age > 30) %>% select(name, age) -> Option D