Unexpected behavior of `dplyr::mutate` with grouped data frames in R 4.3.1
I'm working with a grouped data frame using `dplyr` in R 4.3.1, and I’m encountering unexpected results when trying to create a new column based on existing grouped data. I have a dataset that contains sales data for different products across various regions, and I've grouped this data by `product` and `region`. After grouping, I want to calculate the percentage of total sales for each product within its respective region. However, my calculations seem to be off. Here’s a simplified version of what I’m trying to achieve: ```r library(dplyr) # Sample data frame sales_data <- data.frame( product = c('A', 'A', 'B', 'B', 'C', 'C'), region = c('North', 'South', 'North', 'South', 'North', 'South'), sales = c(100, 150, 200, 100, 300, 100) ) # Group by product and region, then calculate percentage result <- sales_data %>% group_by(product, region) %>% mutate(percent_of_total = sales / sum(sales) * 100) print(result) ``` However, the `percent_of_total` column seems to be returning the same value for each entry within a group rather than the expected percentage of total sales per product within each region. The output looks like this: ```r # A tibble: 6 × 4 # Groups: product, region [6] product region sales percent_of_total <chr> <chr> <dbl> <dbl> 1 A North 100 40 2 A South 150 40 3 B North 200 66.7 4 B South 100 33.3 5 C North 300 75 6 C South 100 25 ``` It seems like the `sum(sales)` is returning the wrong total. I tried using `ungroup()` before the `mutate()` function, but it didn’t help clarify the results. Is there a specific way to ensure that the calculation reflects the total sales per product within each region? I’m concerned that I might be misunderstanding how `group_by()` and `mutate()` interact. Any insights would be greatly appreciated!