How to compute mean in R

mean function in R
mean function in R

In this tutorial, we will learn how to use mean() function in R to compute mean (or average) of a vector.

In the most basic use case, mean() function takes a vector as input argument and computes arithmetic mean of the elements in the vector

# mean of vector "x"
mean(x)

In R, we can learn more about the function with executable examples by typing the following in the console. We can see that mean() can compute mean values of numerical, logical and datetime vectors.

?mean

Here the help page for mean() function looks like this. We can see that mean() function in R can compute mean values of numerical, logical and datetime vectors.

mean function in R

Computing mean with mean() function in R

Let us see a simple example of computing mean value from a numerical vector. We manually create a simple vector with numbers from 1 to 5 and save the vector in the variable name my_vector.

By calling mean() function on our vector, we get the mean value.

# computing arithmetic mean with mean()
my_vector <- c(1,2,3,4,5)
mean(my_vector)

## [1] 3

Computing mean with missing values

Sometimes, our vector of interest my contain one or more NAs, missing values. By default, mean() function does not remove missing values or NAs from the vector. Therefore, applying mean() function to a vector with NAs we get NA as our result.

In this example below, our vector has one NA and mean() function’s out is also NA.

# computing mean with missing values 
my_vector <- c(1,2,3,4,NA)
mean(my_vector)
## [1] NA

To get mean value after removing any NAs/missing values, we need to specify mean function’s argument na.rm=TRUE. With na.rm=TRUE as argument to mean() function, we get a mean value as the function removes NAs before computing mean.

mean(my_vector, na.rm=TRUE)
## [1] 2.5

Computing mean of logical vectors

mean() function in R can also work with logical vectors containing TRUE/FALSE values. Here is an example of a vector containing three TRUE values and applying mean to this vector we get 1 as output.

logical_vector1 <- c(TRUE,TRUE,TRUE)
mean(logical_vector1)

## [1] 1

Here is another example of a logical vector containing three FALSE values and applying mean to this vector we get 0 as output.

logical_vector2 <- c(FALSE, FALSE,FALSE)
mean(logical_vector2)
## [1] 0

In our third example of applying mean() function logical vector, we have logical vector containing both TRUE and FALSE

logical_vector <- c(TRUE, FALSE, TRUE)
mean(logical_vector)
## [1] 0.6666667

We can immediately see that TRUE values considered as ones and FALSE values considered zeros while computing arithmetic mean with mean() function in R.


Computing mean of a column of a matrix in R

We can apply mean() function to compute mean value of a column of a matrix or a column of a dataframe. In this example, we show how to compute mean value of a column of a matrix.

Let us first create data matrix using sample() function and matrix() function in R.

data <- sample(c(1:5), 50, replace = TRUE)
data_mat <- matrix(data, ncol=5)
head(data_mat)

##      [,1] [,2] [,3] [,4] [,5]
## [1,]    3    5    2    1    5
## [2,]    3    3    1    1    5
## [3,]    2    3    3    2    4
## [4,]    2    1    4    3    5
## [5,]    3    4    1    4    2
## [6,]    5    1    3    5    1

To compute mean value of a column we provide the column as input to mean() function. In this example, we compute mean of 2nd column. And we subset second column using slicing operation [,2]

mean(data_mat[,2])

## [1] 2.8

Note applying mean() to whole matrix by mistake might be a problem, as mean() function does not throw error. Instead, it converts the whole matrix as avector and compute mean and probably that is not the mean you wanted.

# incorrect application of mean functio to matrix
mean(data_mat)

## [1] 2.9
Exit mobile version