Calculate mean of a vector in R

In this tutorial, we shall learn to calculate mean of a vector in R

Mean of a vector

Mean of a vector is the average or arithmetic mean of elements in the vector. A built-in function called mean() is used to calculate mean of a vector in R programming language.

Syntax of mean() in R

The syntax of mean() function in R is

</>
Copy
mean(x, trim=0, na.rm = FALSE, ...)

where

  • x could be numeric vector / logical vector / data object / date-time object / time interval. Also x could be complex vector provided time=0.
  • trim range is [0, 0.5]. It is the fraction of elements that would be dropped before calculating mean. trim=0.2 means 20% of elements at the beginning and 20% of the elements at the ending of the vector are removed, and the mean is calculated for the 60% of elements remaining.
  • na.rm mean NA REmoval. It could be TRUE or FALSE. If TRUE, NA values in vector would be stripped out before mean computation proceeds.

Examples to Calculate mean of a vector in R

We shall learn to calculate mean with different options available with mean() function.

Example 1 – mean() of Numeric Vector

In this example, we will find the mean of numeric vector using mean() function.

example.R – R Program

</>
Copy
x = c(1,2,3,4,5,6,7,8,9,45)
xm = mean(x)
c(xm)

Output

[1] 5

Explanation

xm = (1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 45)/9 = 90/10 = 9

Example 2 – mean() of Numeric Vector with trim Attribute

In this example, we will find the mean of numeric vector using mean() function. We will also pass trim attribute to trim off percentage of elements at start and end of the vector.

example.R – R Program

</>
Copy
x = c(1,2,3,4,5,6,7,8,9,45)
xm = mean(x, trim=0.10)
c(xm)

Output

[1] 5.5

trim=0.10 i.e., 10% of elements at starting and ending are removed. 1 and 45 are removed and the mean is calculated with rest of elements in the vector.

Explanation

xm = ( 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9)/9 = 44/8 = 5.5

Example 3 – mean() of Numeric Vector with na.rm=TRUE

In this example, we will find the mean of numeric vector using mean() function. We will also pass the argument na.rm=TRUE to mean() function.

example.R – R Program

</>
Copy
x = c(1,2,3,4,5,6,7,8,9,45,NA)
xm = mean(x, trim=0.0, na.rm=TRUE)
c(xm)

Output

[1] 9

na.rm = TRUE removes all the NA values present in vector before calculation proceeds.

Explanation

xm  =  (2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 45) / 9  =  44 / 8  =  5.5

Example 4 – mean() of Numeric Vector with na.rm=FALSE

In this example, we will find the mean of numeric vector using mean() function. We will also pass the argument na.rm=FALSE to mean() function.

example.R – R Program

</>
Copy
x = c(1,2,3,4,5,6,7,8,9,45,NA)
xm = mean(x, trim=0.0, na.rm=TRUE)
c(xm)

Output

[1] NA

na.rm = FALSE does not remove NA values present in vector before calculation proceeds. And if NA is present in the vector, mean would be NA irrespective of anything else.

Explanation

xm = (1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 45 + NA) / 9 = NA

If NA s are expected in a vector, na.rm has to be considered.

Example 5 – mean() of Logical Vector

In this example, we will find the mean of logical vector using mean() function.

example.R – R Program

</>
Copy
x = c(TRUE, FALSE, FALSE, FALSE, TRUE)
xm = mean(x)
c(xm)

Output

[1] 0.4

For a logical vector, TRUE is considered as 1 and FALSE is considered as 0.

Explanation

xm = (TRUE + FALSE + FALSE + FALSE + TRUE)/5 =(1+0+0+0+1)/5 = 2/5 = 0.4

Conclusion

In this R Tutorial, we have learnt about mean() function and how to Calculate Mean of a Vector in R with Example R Scripts.