How can I calculate the average across columns in R, and can you provide some examples?

Calculating the average across columns in R is a simple and efficient way to analyze data. This process involves finding the mean value of each column in a data set. To do this, you can use the built-in function “colMeans()” in R. It takes a data frame as its argument and returns a vector of the mean values for each column. For example, if you have a data frame with three columns, you can use the “colMeans()” function to find the average for each column. This can be useful for summarizing data and identifying any trends or patterns. Additionally, you can use different functions such as “rowMeans()” or “apply()” to calculate the average across rows or for specific subsets of data. Overall, calculating the average across columns in R can provide valuable insights into your data and aid in making informed decisions.

Average Across Columns in R (With Examples)


Often you may want to calculate the average of values across several columns in R. Fortunately this is easy to do using the rowMeans() function. 

This tutorial shows several examples of how to use this function in practice.

Example 1: Find the Average Across All Columns

The following code shows how to calculate the average value of each row across all columns in a data frame:

#create data frame
data <- data.frame(var1 = c(0, NA, 2, 2, 5),
                   var2 = c(5, 5, 7, 8, 9),
                   var3 = c(2, 7, 9, 9, 7))

#view data frame
data

  var1 var2 var3
1    0    5    2
2   NA    5    7
3    2    7    9
4    2    8    9
5    5    9    7

#find average value in each row
rowMeans(data, na.rm=TRUE)

[1] 2.333333 6.000000 6.000000 6.333333 7.000000

The way to interpret the output is as follows:

  • The average value in the first row is 2.333.
  • The average value in the second row is 6.
  • The average value in the third row is 6.
  • The average value in the fourth row is 6.333.
  • The average value in the fifth row is 7.

You can also assign these row averages to a new variable in the data frame:

#assign row averages to new variable named row_mean
data$row_mean <- rowMeans(data, na.rm=TRUE)

#view data frame
data

  var1 var2 var3 row_mean
1    0    5    2 2.333333
2   NA    5    7 6.000000
3    2    7    9 6.000000
4    2    8    9 6.333333
5    5    9    7 7.000000

Example 2: Find the Average Across Specific Columns

It’s also possible to find the average across only specific columns in a data frame. For example, the following code shows how to calculate the row averages across just the first two columns:

#find row averages across first two columnsdata$new <- rowMeans(data[ , c(1,2)], na.rm=TRUE)

#view data frame
data

  var1 var2 var3 new
1    0    5    2 2.5
2   NA    5    7 5.0
3    2    7    9 4.5
4    2    8    9 5.0
5    5    9    7 7.0

We can see that:

  • The average value in the first row across the first two columns is 2.5.
  • The average value in the second row across the first two columns is 5.

And so on.

You can use similar syntax to find the row averages for any set of columns. For example, the following code shows how to calculate the row averages across just the first and third columns:

#find row averages across first and third columnsdata$new <- rowMeans(data[ , c(1,3)], na.rm=TRUE)

#view data frame
data

  var1 var2 var3 new
1    0    5    2 1.0
2   NA    5    7 7.0
3    2    7    9 5.5
4    2    8    9 5.5
5    5    9    7 6.0
  • The average value in the first row across the first and third columns is 1.
  • The average value in the second row across the first and third columns is 7.

And so on.

You can find more R tutorials here.

x