# Sample data frame
bike_orderline_tbl <- data.frame(
category_1 = c("A", "A", "B", "B", "C"),
category_2 = c("X", "Y", "X", "Y", "X"),
frame_material = c("Aluminum", "Steel", "Aluminum", "Steel", "Aluminum"),
Sales = c(100, 150, 120, 200, 80)
)
# 1. Use 'bike_orderline_tbl'.
# 2. Group and summarize the data, calling the new column 'Sales'.
# You need to specify the grouping variables and the summarization function.
# In this example, we'll group by 'category_1', 'category_2', and 'frame_material',
# and calculate the total sales for each group.
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
result <- bike_orderline_tbl %>%
group_by(`category_1`, `category_2`, `frame_material`) %>%
summarise(Sales = sum(Sales))
## `summarise()` has grouped output by 'category_1', 'category_2'. You can
## override using the `.groups` argument.
# 3. Format the 'Sales' column as 'dollars()'.
result$Sales <- scales::dollar(result$Sales)
# 4. Rename the columns as specified.
result <- result %>%
rename(`Prime category` = `category_1`,
`Secondary category` = `category_2`,
`Frame Material` = `frame_material`)
# View the result
print(result)
## # A tibble: 5 × 4
## # Groups: Prime category, Secondary category [5]
## `Prime category` `Secondary category` `Frame Material` Sales
## <chr> <chr> <chr> <chr>
## 1 A X Aluminum $100
## 2 A Y Steel $150
## 3 B X Aluminum $120
## 4 B Y Steel $200
## 5 C X Aluminum $80