What's the Best Way to Calculate Percentages in R When Working With Grouped Data?

Hi everyone,

I'm interested in learning the best way to calculate percentages when working with grouped datasets in R.

For example, suppose I have sales data grouped by product category and want to calculate the percentage contribution of each category to total sales. I know this can be done by dividing each category's total by the overall total and multiplying by 100.

I'm curious about the approaches people prefer for this kind of task. Would you recommend using dplyr with group_by() and summarise(), or is there a more efficient approach for larger datasets?

I'd also like to understand how you handle missing values and zero totals to avoid unexpected results.

If anyone has a simple, reproducible example, I'd appreciate seeing it. Thanks for sharing your experience!

It is a little faster to use the .by within mutate.

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
library(gapminder)

bench::mark(
  mutby = gapminder |>
    mutate(
      .by = year,
      PctPop = pop / sum(pop) * 100
    ) |>
    arrange(year, country),
  mutgby = gapminder |>
    group_by(year) |>
    mutate(
      PctPop = pop / sum(pop) * 100
    ) |>
    ungroup() |>
    arrange(year, country)
)
#> # A tibble: 2 × 6
#>   expression      min   median `itr/sec` mem_alloc `gc/sec`
#>   <bch:expr> <bch:tm> <bch:tm>     <dbl> <bch:byt>    <dbl>
#> 1 mutby        2.45ms   3.64ms      253.    2.86MB     13.0
#> 2 mutgby       3.83ms   5.64ms      170.    1.21MB     11.3

Created on 2026-10-29 with reprex v2.1.1

I think there's one important distinction here: are you calculating each row's percentage within its category, or each category's share of total sales?

The .by approach mentioned above is useful, but for the second case I'd aggregate the categories first and then calculate the percentages across the resulting totals.

For example:

library(dplyr)

sales <- tibble(
  category = c("A", "A", "B", "C"),
  amount = c(100, NA_real_, 60, 0)
)

sales |>
  summarise(
    category_sales = sum(amount, na.rm = TRUE),
    .by = category
  ) |>
  mutate(
    pct_total = if_else(
      sum(category_sales) == 0,
      NA_real_,
      100 * category_sales / sum(category_sales)
    )
  )

That gives A = 62.5%, B = 37.5%, and C = 0%.

One thing I'd be careful about is na.rm = TRUE. It works well if missing sales should simply be excluded, but a category where all values are missing will also get a total of zero. Depending on the data, that might be misleading. I'd probably flag those categories separately if missing data has a business meaning.

Also, returning NA when the overall total is zero seems safer than forcing a percentage, since there's no meaningful denominator.

For larger datasets, I'd benchmark both approaches on the actual data. The number of groups and whether you need category-level totals or row-level percentages may matter more than a small difference between group_by() and .by.