Hey everyone,
I'm trying to learn dplyr and I'm stuck on what seems like a simple problem. I have a data frame sales_data with columns for product_category, region, and sales_total.
# Sample Data
sales_data <- data.frame(
product_category = c("Electronics", "Clothing", "Electronics", "Books", "Clothing"),
region = c("North", "North", "South", "South", "West"),
sales_total = c(1200, 300, 800, 150, 450)
)
I want to calculate the total sales for each product_category. I thought this code would work, but it's just returning the same number of rows as my original data frame.
library(dplyr)
sales_data %>%
group_by(product_category) %>%
mutate(total_sales = sum(sales_total))
What am I doing wrong? I expected to get one row for "Electronics", one for "Clothing", and one for "Books". Thanks in advance!