hope-data-science/tidyfst

Multiple functions in summarise_vars to be supported

hope-data-science opened this issue · 1 comments

A friend has suggested that summarise_vars should support multiple functions, I have proposed the prototype, which should be extended to a function later.

library(tidyfst)
iris %>% as.data.table()->DT

foo <- function(x){
  list(mn = mean(x), sd = sd(x), med = median(x))
}
# DT[, as.list(unlist(lapply(.SD, foo))), by = .(Species)]

DT %>% 
  summarise_vars(is.numeric,.func = foo,by = Species) %>% 
  mutate_vars(is.list,unlist) %>% 
  mutate_dt(class = rep(c("mn","sd","med"),uniqueN(Species)))


Solved. Try:

# use multiple functions on multiple columns
iris %>%
  summarise_vars(is.numeric,.func = list(mean,sd,median))
iris %>%
  summarise_vars(is.numeric,.func = list(mean,sd,median),by = Species)