Similar to here:
I'm wanting to summarize multiple classes of columns in a single summarise_all() call, with the caveat that sometimes I want even more control than that, to be able to summarise all numerics one way, most character columns with paste(collapse) and some with first.
In this example,
library(dplyr)
data(iris)
iris$year <- rep(c(2000,3000),each=25) ## for grouping
iris$color <- rep(c("red","green","blue"),each=50) ## character column
iris %>%
group_by(Species, year) %>%
summarise_all(funs(if(is.numeric(.)) mean(., na.rm = TRUE) else first(.)))
Now, what if I want to take the first element of Species, but I want to keep all instances of color?
I have to do them one at a time and them combine them with a join.
colors <-
iris %>%
group_by(Species, year) %>%
summarise_at(vars(color), ~paste(.x, collapse="; "))
others <-
iris %>%
select(-color) %>% group_by(Species, year) %>%
summarise_all(funs(if(is.numeric(.)) mean(., na.rm = TRUE) else first(.)))
full_join(colors, others)
Is there a way to do numerics and a mixture of functions for specific columns?