简体   繁体   中英

Incorporating na.rm=TRUE into Summarise_Each for Multiple Functions in dplyr

So I have a dplyr table movie_info_comb from which I am calculating various statistics on one column metascore. Here is the code:

summarise_each_(movie_info_comb, funs(min,max,mean,sum,sd,median,IQR),"metascore")

How do incorporate na.rm=TRUE? I've only seen examples for which one statistic is being calculated and I'd hate to have to repeat this 5 times (one for each function.

Thanks in advance.

You can do this with lazy evaluation

library(lazyeval)

na.rm = function(FUN_string)
  lazy(FUN(., na.rm = TRUE)) %>%
    interp(FUN = FUN_string %>% as.name)

na.rm.apply = function(FUN_strings)
  FUN_strings %>% 
  lapply(na.rm) %>%
  setNames(FUN_strings)

mtcars %>%
  select(mpg) %>%
  summarize_each(
    c("min","max","mean","sum","sd","median","IQR") %>%
      na.rm.apply)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM