new column mutate by group summary


Does anyone know do a summary by group? I have the below dataframe and it comes with totals already.

I want to create a new column, using mutate, that will sum those trees together. Like this image below.


I tried ungroup() and doing another mutate but it doesn't seem to work. I've found a work around by creating a new summary by Counties only and doing a simple join but it has to be easier.

Trees = data.frame(
  stringsAsFactors = FALSE,
      Trees_COUNTY = c("Thunder Bay","Thunder Bay",
                       "Thunder Bay","Frontenac","Frontenac","Frontenac",
            Stores = c("A", "B", "C", "A", "B", "C", "A", "B", "C"),
             Trees = c(761L, 1581L, 17201L, 351L, 421L, 15771L, 1891L, 291L, 14211L)
         Trees_COUNTY) %>% mutate(
           sum_at_county_level = sum(Trees)
1 Like

Thanks for the quick reply, it's even faster than my edit to the post. I'll give it a try. :slight_smile:

Works great thanks!

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.