Calculate the mean of every column & exclude NA’s
In this example, the user has to use the colmean() function with the na.rm argument to calculate the mean of a column by excluding NA. NA stands for Not a number, we can do this by using na.rm() method, we will set it to True to remove NA values in the dataframe column.
Syntax:
colMeans(dataframe,na.rm=TRUE)
Example:
In this example, we will create three columns that include three NA values and get the mean of all columns using the na.rm argument under the colmeans() function.
R
# create dataframe with three columns data= data.frame (col1= c (1,34,56,32,23, NA , NA , NA ), col2= c (21, NA , NA , NA ,34,56,32,34), col3= c (1:5, NA , NA , NA )) # get mean of all columns excluding NA print ( colMeans (data,na.rm= TRUE )) |
Output:
col1 col2 col3 29.2 35.4 3.0
How to Use ColMeans Function in R?
In this article, we will discuss how to use the ColMeans function in R Programming Language.