📖 Scenario: You have sales data from a small store. Each sale has a product name and the number of units sold. You want to find out how many units were sold for each product in total.
🎯 Goal: Build a small R script that groups sales by product and calculates the total units sold for each product using group_by() and summarise().
📋 What You'll Learn
Create a data frame called sales with columns product and units with exact values
Create a grouped data frame using group_by() on product
Use summarise() to calculate total units sold per product
Print the summarised data frame
💡 Why This Matters
🌍 Real World
Stores and businesses often need to summarise sales data by product to understand which items sell the most.
💼 Career
Data analysts and scientists use <code>dplyr</code> functions like <code>group_by()</code> and <code>summarise()</code> to prepare and analyse data efficiently.
Progress0 / 4 steps
1
Create the sales data frame
Create a data frame called sales with two columns: product and units. Use these exact values: product = c("apple", "banana", "apple", "orange", "banana"), units = c(5, 3, 2, 4, 1).
R Programming
Hint
Use data.frame() with named vectors for product and units.
2
Group sales by product
Load the dplyr package with library(dplyr). Then create a new variable called grouped_sales by applying group_by() on the sales data frame grouping by product.
R Programming
Hint
Use library(dplyr) to load the package first.
3
Summarise total units sold per product
Create a new variable called summary_sales by applying summarise() on grouped_sales. Calculate the total units sold per product using total_units = sum(units).
R Programming
Hint
Use summarise() with total_units = sum(units) inside.
4
Print the summarised sales data
Print the summary_sales data frame to show total units sold per product.
R Programming
Hint
Use print(summary_sales) to display the result.
Practice
(1/5)
1. What does the group_by() function do in R's dplyr package?
easy
A. It splits data into groups based on specified columns.
B. It sorts the data in ascending order.
C. It removes duplicate rows from the data.
D. It changes the data type of columns.
Solution
Step 1: Understand the purpose of group_by()
The group_by() function is used to split data into groups based on one or more columns.
Step 2: Compare with other options
Sorting, removing duplicates, or changing data types are done by other functions, not group_by().
Final Answer:
It splits data into groups based on specified columns. -> Option A
Quick Check:
group_by() = splits data into groups [OK]
Hint: Remember: group_by() groups data by columns [OK]
Common Mistakes:
Confusing group_by() with arrange() for sorting
Thinking group_by() removes duplicates
Assuming group_by() changes data types
2. Which of the following is the correct syntax to group a data frame df by the column category and then summarise the mean of value?
easy
A. df %>% group_by(category) %>% summarise(mean_value = mean(value))
B. df %>% summarise(mean_value = mean(value)) %>% group_by(category)
C. group_by(df, category) %>% summarise(mean_value = mean(value))
D. df %>% group_by(category) %>% mean(value)
Solution
Step 1: Identify correct pipe sequence
The correct order is to first group the data, then summarise it. So df %>% group_by(category) %>% summarise(...) is correct.
Step 2: Check function usage
mean(value) inside summarise is correct. df %>% summarise(mean_value = mean(value)) %>% group_by(category) reverses the order, C uses wrong syntax, and D misses summarise.
Final Answer:
df %>% group_by(category) %>% summarise(mean_value = mean(value)) -> Option A
A. The code will produce an error because group_by() must come first.
B. The summarise() is used before group_by(), so grouping is lost.
C. The mean() function is used incorrectly inside summarise().
D. The data frame df is missing required columns.
Solution
Step 1: Analyze function order
The code summarises the whole data first, losing group info, then tries to group after summarising.
Step 2: Understand grouping effect
Grouping after summarise has no effect because summarise returns a smaller data frame without original groups.
Final Answer:
The summarise() is used before group_by(), so grouping is lost. -> Option B
Quick Check:
group_by() must come before summarise() [OK]
Hint: Always group before summarising to keep groups [OK]
Common Mistakes:
Assuming group_by() after summarise() works
Thinking summarise() keeps original groups
Believing mean() usage is wrong here
5. You have a data frame sales with columns region, product, and revenue. You want to find the average revenue per product only for regions where total revenue exceeds 1000. Which code correctly achieves this?