I have to calculate the mean of the columns in a dataframe by writing a function and then applying it. I understand that this is easy to do with mean
and
Why not to use dplyr
?
You can get the mean for all columns in your data.frame using
summarise_each(funs(mean))
If we apply it to mtcars
library(dplyr)
mtcars %>% summarise_each(funs(mean))
# mpg cyl disp hp drat wt qsec vs am gear carb
#1 20.09062 6.1875 230.7219 146.6875 3.596563 3.21725 17.84875 0.4375 0.40625 3.6875 2.8125