Compute Mean of Data Frame Column in R (6 Examples)
This article illustrates how to calculate the mean of a data frame column in the R programming language.
Table of contents:
Let’s just jump right in.
Creation of Example Data
First, I’ll need to create some data that we can use in the examples below:
data <- data.frame(x1 = 1:5, # Create example data x2 = 9:5, x3 = 5) data # Print example data # x1 x2 x3 # 1 1 9 5 # 2 2 8 5 # 3 3 7 5 # 4 4 6 5 # 5 5 5 5
The previous output of the RStudio console shows that our example data consists of five rows and three columns. All variables of our data frame have the class numeric.
Example 1: Calculate Mean of Variable Using $-Operator
In Example 1, I’ll explain how to use the $-operator to get the mean of a column in R.
mean(data$x2) # Apply $-operator # 7
The previous output of the RStudio console shows the result: The mean of the column x2 is 7.
Example 2: Calculate Mean of Variable Using []
The R programming language provides many alternatives for the computation of mean values. The following syntax illustrates how to use square brackets to return the mean of a variable.
mean(data[["x2"]]) # Apply square brackets # 7
The result is the same as in Example 1.
Example 3: Calculate Mean of Variable Using Column Index
In Example 3, I’ll show how to use column indices in a data frame to calculate the average of a variable.
mean(data[ , 2]) # Specify index position # 7
Again, the output of the RStudio console is 7.
Example 4: Calculate Mean of Variable Using dplyr Package
The following R programming syntax shows how to use the dplyr package to compute averages.
We first have to install and load the dplyr package, if we want to use the corresponding functions:
install.packages("dplyr") # Install dplyr package library("dplyr") # Load dplyr
Now, we can use the summarise function of the dplyr package to return a data matrix containing our mean value:
summarise(data, my_mean = mean(x2)) # Apply summarise function # my_mean # 1 7
Example 5: Calculate Mean of All Variables Using colMeans Function
In this Example, I’ll explain how to return the means of all columns using the colMeans function.
colMeans(data) # Apply colMeans function # x1 x2 x3 # 3 7 5
The previous output of the RStudio console shows the mean values for each column, i.e. the mean of the variable x1 is 3, the mean of the variable x2 is 7, and the mean of the variable x3 is 5.
Example 6: Calculate Mean of Variable with Missing Values
This Example illustrates how to handle missing value (i.e. NA values) when computing means of columns in R. For this, we first have to create some example data with missing values:
data_NA <- data # Create example data with NA data_NA$x2[c(1, 3)] <- NA data_NA # Print example data with NA # x1 x2 x3 # 1 1 NA 5 # 2 2 8 5 # 3 3 NA 5 # 4 4 6 5 # 5 5 5 5
Now, we can use the na.rm argument within the mean function to exclude missing values from our calculation:
mean(data_NA$x2, na.rm = TRUE) # Specify na.rm argument # 6.333333
Video & Further Resources
Would you like to learn more about computing means in R? Then you may want to watch the following video of my YouTube channel. In the video, I’m illustrating the contents of this page in RStudio.
Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.
If you accept this notice, your choice will be saved and the page will refresh.
Furthermore, I can recommend having a look at the related tutorials of this website. You can find a selection of related articles about topics such as missing data, naming data, and descriptive statistics below.
In summary: In this article you learned how to compute the average of one or multiple variables in R programming. Let me know in the comments, if you have additional questions and/or comments. Furthermore, please subscribe to my email newsletter in order to receive updates on new tutorials.
Statistics Globe Newsletter