Search code examples
rdplyrsummarize

Aggregation in R to calculate percentage of total by group?


DDD <- summarise(
  group_by(Customers, Last_region, Last_state, Last_city),     
  Count = length(Last_city),
  Total = sum(Customer.Value, na.rm = TRUE),
  Percent = sum(Customer.Value * 100 / sum(Customer.Value, na.rm = TRUE)))       

I have tried this code.I get the group by for total & count but not for percent ?


Solution

  • We need to change to Customers$Customer.Value and also for better understanding, use the %>% instead of using nested functions.

    Customers %>%    
       group_by(Last_region, Last_state, Last_city) %>%     
       summarise(Count = length(Last_city),
                 Total = sum(Customer.Value, na.rm = TRUE),
                 Percent = sum(Customer.Value * 100 / 
                              sum(Customers$Customer.Value, na.rm = TRUE)))       
    

    As the OP didn't show any reproducible example, we also have a doubt of the closing bracket in sum(Customer.Value*100