2
votes

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.

1

1 Answers

7
votes

We neeed to use

df %>%
   summarise_all(funs(mean(.[.!=0])))