Home » How to Calculate the Mean of Multiple Columns in R

How to Calculate the Mean of Multiple Columns in R

by Erma Khan

Often you may want to calculate the mean of multiple columns in R. Fortunately you can easily do this by using the colMeans() function.

colMeans(df)

The following examples show how to use this function in practice.

Using colMeans() to Find the Mean of Multiple Columns

The following code shows how to use the colMeans() function to find the mean of every column in a data frame:

#create data frame
df #find mean of each column
colMeans(df)

var1 var2 var3 var4 
 3.2  5.4  5.2  4.2 

We can also specify which columns to find the mean for:

#find the mean of columns 2 and 3
colMeans(df[ , c(2, 3)])

var2 var3 
 5.4  5.2 

#find the mean of the first three columns
colMeans(df[ , 1:3])

var1 var2 var3 
 3.2  5.4  5.2

If there happen to be some columns that aren’t numeric, you can use sapply() to specify that you’d only like to find the mean of columns that are numeric:

#create data frame
df #find mean of only numeric columns
colMeans(df[sapply(df, is.numeric)])

var1 var2 var3 var4 
 3.2  5.4  5.2  4.2 

And if there happen to be missing values in any columns, you can use the argument na.rm=TRUE to ignore missing values when calculating the means:

#create data frame with some missing values
df #find mean of each column and ignore missing values
colMeans(df, na.rm=TRUE)

var1 var2 var3 var4 
 3.0  5.4  5.2  3.0

Additional Resources

How to Loop Through Column Names in R
How to Sum Specific Columns in R

Related Posts