3

I want to use dplyr's new across syntax with a list of functions removing NAs.

This removes NAs, but I don't know how to do multiple functions at a time without rewriting.

library(tidyverse)
iris$Sepal.Length[1] <- NA #Creating an NA
iris %>%
  group_by(Species) %>% 
  summarise(across(c(Sepal.Length:Petal.Width), ~ mean(.x, na.rm = TRUE)))

This does multiple functions at a time, but I don't know how to modify it to remove NAs.

iris %>%
  group_by(Species) %>% 
  summarise(across(c(Sepal.Length:Petal.Width), list(mean = mean, sd = sd)))
Jack Landry
  • 138
  • 8

1 Answers1

4

One option could be:

iris %>%
 group_by(Species) %>% 
 summarise(across(c(Sepal.Length:Petal.Width), 
                  list(mean = ~ mean(., na.rm = TRUE), sd = ~ sd(., na.rm = TRUE))))
tmfmnk
  • 38,881
  • 4
  • 47
  • 67