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 #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 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 columns data$new 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 columns data$new 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
We can see that:
- 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.