3.1 C
London
Friday, December 20, 2024
HomeRDescriptive Statistics in RHow to Calculate the Mean of Multiple Columns in R

How to Calculate the Mean of Multiple Columns in R

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

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

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories