How do I use the aggregate function to mean|count|sum up multiple columns in R?

Try

library(dplyr)
library(tidyr)

df %>%
  gather(key, value, -Gender) %>%
  group_by(Gender) %>%
  summarise(Count = n(), Percentage_Count = n() / nrow(.), 
            Total_Donation = sum(value),
            Percentage_Donation = sum(value) / sum(.$value), 
            Mean_Donation = mean(value)) 

Which gives:

# A tibble: 2 x 6
#  Gender Count Percentage_Count Total_Donation Percentage_Donation Mean_Donation
#  <fctr> <int>            <dbl>          <int>               <dbl>         <dbl>
#1      F     9              0.5         124500           0.5071283      13833.33
#2      M     9              0.5         121000           0.4928717      13444.44

Leave a Comment