1

I have a data frame with columns - ID, Var_1, Var_2..., where id - identifier, Var_i - variable.

So, the purpose is to apply some functions such as min, max, mean for each columns, grouping by ID, and to get the data like that -

ID -- min_Var_1 -- mean_Var_1 -- max_Var_1 -- min_Var_2 -- mean_Var_2....

The problem is I have more than 100 variables. How can I do this efficiently?

db <- data %>%
    group_by(N_det) %>%
    summarise(min_Var_1 = min(Var_1), mean_Var_1 = mean(Var_1), 
    max_Var_1 = max(Var_1), min_Var_2 = min(Var_2), 
    mean_Var_2 = mean(Var_2)...)
camille
  • 16,432
  • 18
  • 38
  • 60
  • Note that the duplicate link showed is using deprecated options from dplyr. – akrun Apr 03 '19 at 14:40
  • @akrun which options? The 2 highest-voted answers use `summarise_all`, `summarise_if`, and/or `summarise_at`. One of them mentions that `summarise_each` has been deprecated. I was trying to find a post that included newer methods to pass lists of functions—it might be helpful to add answers such as yours to those older posts. – camille Apr 03 '19 at 14:51
  • @camille if you haven't checked the use of `funs` vs `list` and deprecation, please chek – akrun Apr 03 '19 at 14:52
  • 1
    @akrun Good point. I think your answer here would provide a good update to older questions, and I've upvoted it for that reason. Your answer doesn't duplicate previous answers I've found, but the question itself is essentially the same as a few older ones. – camille Apr 03 '19 at 14:57
  • @camille There is a github [issue](https://github.com/tidyverse/dplyr/issues/3433) where it is mentioned. – akrun Apr 03 '19 at 14:58
  • @Jaap None of the dupe links show the format I showed here. – akrun Apr 04 '19 at 01:07

1 Answers1

1

We can use summarise_at

library(dplyr)
data %>%
   group_by(N_det) %>%
   summarise_at(vars(starts_with("Vars")), list(min = ~ min(., na.rm = TRUE), 
               max = ~ max(., na.rm = TRUE),
               mean = ~ mean(., na.rm = TRUE)))

A reproducible example with iris

 iris %>% 
   group_by(Species) %>% 
   summarise_at(vars(ends_with("Width")),
      list(min = ~ min(., na.rm = TRUE), max = ~ max(., na.rm = TRUE)))
akrun
  • 874,273
  • 37
  • 540
  • 662