mean Function in R (4 Examples)

 

In this article you’ll learn how to compute the mean in R.

The tutorial is mainly based on the mean() function. Let’s have a look at the basic R syntax and the definition of the mean function first:

 

Basic R Syntax of mean():

mean(x)

 

Definition of mean():

The mean R function computes the arithmetic mean of a numeric input vector.

 

In the following, I’ll explain in four examples how to apply the mean function in R.

Let’s move on to the examples!

 

Example 1: Basic Application of mean() in R

First, let’s create a numeric example vector, to which we can apply the mean R function:

x1 <- c(8, 6, 8, 3, 5, 2, 0, 5)   # Create example vector

We can now apply the mean function to this vector as follows:

mean(x1)                          # Apply mean function in R
# 4.625

Based on the RStudio console output we can see: The mean of our vector is 4.625.

This was easy… But wait, there might occur problems. Keep on reading!

 

Example 2: Handle NA Values with mean Function

A typical problem occurs when the data contains NAs. Let’s modify our example vector to simulate such a situation:

x2 <- c(x1, NA)                   # Create example vector with NA
x2                                # Print vector to RStudio console
# 8  6  8  3  5  2  0  5 NA

Our new example vector looks exactly the same as the first example vector, but this time with an NA value at the end. Let’s see what happens when we apply the mean function as before:

mean(x2)                          # mean function returns NA
# NA

The RStudio console returns NA – not as we wanted. Fortunately, the mean function comes with the na.rm (i.e. NA remove) option, which can be used to ignore NA values. Let’s do this in practice:

mean(x2, na.rm = TRUE)            # Use na.rm option
# 4.625

As you can see, we get the same mean output as before.

Note: The na.rm option can also be used to ignore NaN or NULL values.

 

Example 3: trim Option of mean Function

A less often used option of the mean command is the trim option. The trim option can be used to trim the fraction of observations from each end of our input data before the average is computed. Values of trim outside that range are then taken as the nearest endpoint.

Let’s use our first example vector for illustration:

mean(x1, trim = 0.2)              # Use trim option
# 4.833333

If we specify trim to be equal to 0.2, the mean function returns 4.833333.

 

Example 4: Apply mean Function to Real Data

So far, we have only used a simplified example vector. This example shows how to apply the mean function to the column of a real data set.

For the example, I’m going to use the Iris data set, which can be loaded to RStudio as follows:

data(iris)                        # Load Iris data
head(iris)                        # Head of Iris data

 

nrow function in R - Iris Example Data Frame

Table 1: First Six Rows of the Iris Flower Data Matrix.

 

If we now want to extract the mean of the first column of the Iris data, we can use the following R code:

mean(iris$Sepal.Length)           # Mean of first column
# 5.843333

The mean of the column Sepal.Length is 5.843333.

 

Further Resources & Summary

This tutorial illustrated some of the most important functionalities of the mean function. Since the mean is such an important metric in statistical research and data science, there are many other ways in which the mean function could be applied.

However, I didn’t want to blow up this tutorial too much and have therefore created several other tutorials containing more complex applications of the mean function or other related R commands. You can find a list of these R tutorials below:

Furthermore, you might be interested to learn more about the theoretical research concept of the mean. In this case, I recommend having a look at the following video of the mathantics YouTube channel. In the video, the speaker is not only explaining the mean, but also the related measures median and mode.

 

 

In addition, you could also have a look at some of the more general R tutorials of my website:

In summary: I hope that you know how to deal with the mean function in the R programming language at this point. However, if you have any questions or comments, please let me know in the comments section below.

 



Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top