R中的聚合以按组计算总数的百分比?

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)))       

我试过这个 code.I 获取分组依据是总计和计数而不是百分比?

我们需要更改为 Customers$Customer.Value 并且为了更好地理解,请使用 %>% 而不是使用嵌套函数。

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)))       

由于 OP 没有显示任何可重现的示例,我们也对 sum(Customer.Value*100 中的右括号有疑问