Reputation: 690
I would like to do this:
data %>%
group_by(ID) %>%
summarize(maxVal = max(Val),
maxVal2 = max(Val2))
However, I have a lot of columns I would like to get the Max of. I would like to pass in a vector of columns like this:
cols <- c("Val", "Val2")
data %>%
group_by(ID) %>%
summarize(max(cols))
However, this does not work. How do I fix the syntax to do this easily?
Upvotes: 1
Views: 574
Reputation: 887991
If we wanted to have a prefix name after summarize
ing the multiple columns, then use rename_at
library(tidyverse)
data %>%
group_by(ID) %>%
summarise_at(vars(cols), max) %>%
rename_at(-1, ~ paste0('max', .))
As a reproducible example, used the data(mtcars)
mtcars %>%
group_by(gear) %>%
summarise_at(vars(mpg, disp), max) %>%
rename_at(-1, ~ paste0('max', .))
# A tibble: 3 x 3
# gear maxmpg maxdisp
# <dbl> <dbl> <dbl>
#1 3 21.5 472
#2 4 33.9 168.
#3 5 30.4 351
Upvotes: 1