Search code examples
rdplyr

Can I calculate the standard error of all columns with the "summarise_all" function in R dplyr


I am preparing course material for the dplyr in R. Assuming that our data set is "iris" someone can calculate the mean and sd of all columns with the summarise all function

iris %>% 
  group_by(Species) %>% 
  summarise_all(funs(mean, sd), na.rm=TRUE)

Howeveer when I am trying to calulate the standard error I am taking an error message.

iris %>% 
  group_by(Species) %>% 
  summarise_all(funs(mean, sd, se = sd/sqrt(n)), na.rm=TRUE)

Any help is highly appreciated


Solution

  • You can use :

    library(dplyr)
    iris %>% 
      group_by(Species) %>% 
      summarise_all(list(mean = ~mean(.), sd = ~sd(.), se = ~sd(./sqrt(.))))
    

    Or probably shorter but doesn't give you the column names you desire :

    iris %>% group_by(Species) %>% summarise_all(list(mean, sd, se = ~sd(./sqrt(.))))