rdataframegroup-bydplyrsummarize

R group_by multiple conditionnal aggregation functions dplyr


I am searching to compute functions on a group conditionally.

agg_name describes functions to evaluate, this vector can be length 1 to N. Here N = 5 and all possibilities are knew but I would prefer something general.

agg_name <- c("Sum = sum(val)", "Mean = mean(val)")

df <- data.frame(dateDay = Sys.Date()-rep(0:2, each = 3), val = 1:9)

Attempt so far :

df <- df %>% 
  group_by(dateDay) %>% 
  summarise(eval(paste(agg_name, collapse = ",")))

Expected output :

     dateDay Sum Mean
1 2019-09-04   6    2
2 2019-09-03   15   5
3 2019-09-02   24   8

An other column would have been created if agg_name contains Max = max(val).

Any help would be greatly appreciated.


Solution

  • You can do:

    agg_name <- c(Sum = sum, Mean = mean)
    
    df %>% 
     group_by(dateDay) %>% 
     summarise_all(agg_name)
    
      dateDay      Sum  Mean
      <date>     <int> <dbl>
    1 2019-09-02    24     8
    2 2019-09-03    15     5
    3 2019-09-04     6     2