2

I would like to apply the dplyr function summarise_all to calculate the mean in each column. However, I do want to omit the 0 values, thus need to build in a conditional statment.

df <- data.frame(x = c(1,0,4,6,0,9), y = c(12,42,8,0,11,2))
df %>% summarise(mean)

Here it is explained how to do when definig the aggrecation fucntion for each single column. However, I would like to use summarise_all, since my originla data contain many columns.

Community
  • 1
  • 1
Bushroot
  • 259
  • 1
  • 13

1 Answers1

7

We neeed to use

df %>%
   summarise_all(funs(mean(.[.!=0])))
akrun
  • 874,273
  • 37
  • 540
  • 662