I am working on R in R studio. I need to calculate the mean for each column of a data frame.
cluster1 // 5 by 4 data frame
mean(cluster1) //
I got :
Warning message:
In mean.default(cluster1) :
argument is not numeric or logical: returning NA
But I can use
mean(cluster1[[1]])
to get the mean of the first column.
How to get means for all columns ?
Any help would be appreciated.
In case you have NA's:
Remember that "mean" needs numeric data. If you have mixed class data, then use:
You can try this:
Another way is to use purrr package
@A Handcart And Mohair
For diversity: Another way is to converts a vector function to one that works with data frames by using
plyr::colwise()
You can use
colMeans
:You can use 'apply' to run a function or the rows or columns of a matrix or numerical data frame: