Calculate Mean of a Vector in R Using mean()
In R, the arithmetic mean of a vector is calculated with the built-in mean() function. The function adds the numeric values and divides the result by the number of values considered. This tutorial explains the syntax, the effect of trim, how to handle NA values, and how logical vectors are averaged.
- Mean of a vector in R
- Examples of mean() for R vectors
- Why mean() returns NA in R
- Checklist for calculating vector mean in R
- FAQs on mean() and vectors in R
Mean of a Vector in R
Mean of a vector is the average or arithmetic mean of the values in that vector. In R programming, mean() is the standard function used for this calculation.
For a numeric vector c(1, 2, 3, 4), the arithmetic mean is calculated as:
(1 + 2 + 3 + 4) / 4 = 10 / 4 = 2.5
The same calculation in R is:
mean(c(1, 2, 3, 4))
[1] 2.5
Syntax of mean() Function in R
The syntax of mean() function in R is:
mean(x, trim=0, na.rm = FALSE, ...)
Arguments Used by mean() for Vector Average
The most commonly used arguments are x, trim, and na.rm.
- x is the object whose mean has to be calculated. For this tutorial,
xis mainly a numeric or logical vector. - trim is a fraction between
0and0.5. It removes that fraction of values from both ends after sorting the values, and then computes the mean of the remaining values. - na.rm tells R whether missing values should be removed before computing the result. The default is
FALSE. UseTRUEwhen the vector may containNAand you want the average of the available values. - … passes additional arguments to methods when
mean()is used with other R objects.
Examples to Calculate Mean of a Vector in R
The following examples show how mean() works with numeric vectors, trimmed values, missing values, and logical vectors.
Example 1 – mean() of Numeric Vector
In this example, we will find the mean of a numeric vector using mean() function.
example.R – R Program
x = c(1,2,3,4,5,6,7,8,9,45)
xm = mean(x)
c(xm)
Output
[1] 9
Explanation
xm = (1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 45) / 10 = 90 / 10 = 9
The vector has 10 values. R adds all values and divides by 10, so the result is 9.
Example 2 – mean() of Numeric Vector with trim Attribute
In this example, we will find the mean of a numeric vector using mean() function. We will also pass trim attribute to trim a fraction of elements from the start and end after sorting.
example.R – R Program
x = c(1,2,3,4,5,6,7,8,9,45)
xm = mean(x, trim=0.10)
c(xm)
Output
[1] 5.5
Here, trim=0.10 means 10% of the values are removed from each end. Since the vector has 10 values, one value is removed from the lower end and one value is removed from the upper end. The values 1 and 45 are removed, and the remaining 8 values are used.
Explanation
xm = (2 + 3 + 4 + 5 + 6 + 7 + 8 + 9) / 8 = 44 / 8 = 5.5
Example 3 – mean() of Numeric Vector with na.rm=TRUE
In this example, the vector contains a missing value represented by NA. We will pass na.rm=TRUE to remove missing values before calculating the mean.
example.R – R Program
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 NA values before calculation. The remaining numeric values are 1,2,3,4,5,6,7,8,9,45.
Explanation
xm = (1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 45) / 10 = 90 / 10 = 9
Example 4 – mean() of Numeric Vector with na.rm=FALSE
In this example, we will use the default missing-value behavior. When na.rm=FALSE, R does not remove NA values before calculation.
example.R – R Program
x = c(1,2,3,4,5,6,7,8,9,45,NA)
xm = mean(x, trim=0.0, na.rm=FALSE)
c(xm)
Output
[1] NA
Since the vector contains NA and na.rm is FALSE, the result is NA. Use na.rm=TRUE when missing values should be ignored.
Explanation
xm = mean(c(1, 2, 3, 4, 5, 6, 7, 8, 9, 45, NA), na.rm = FALSE)
# Result: NA
If NA values are expected in a vector, decide whether those missing values should be removed before computing the mean.
Example 5 – mean() of Logical Vector
In this example, we will find the mean of a logical vector using mean() function.
example.R – R Program
x = c(TRUE, FALSE, FALSE, FALSE, TRUE)
xm = mean(x)
c(xm)
Output
[1] 0.4
For a logical vector, TRUE is treated as 1 and FALSE is treated as 0. Therefore, the mean of a logical vector is the proportion of TRUE values.
Explanation
xm = (TRUE + FALSE + FALSE + FALSE + TRUE) / 5
= (1 + 0 + 0 + 0 + 1) / 5
= 2 / 5
= 0.4
Why mean() Returns NA in R
A common issue while calculating vector mean in R is getting NA as the result. This usually happens when the vector contains at least one missing value and na.rm is not set to TRUE.
x = c(10, 20, NA, 40)
mean(x)
[1] NA
To calculate the mean of the available numeric values, use:
mean(x, na.rm = TRUE)
[1] 23.33333
The result is calculated as:
(10 + 20 + 40) / 3 = 70 / 3 = 23.33333
Calculate Column Means in R Data Frames
When working with a data frame, mean() can be applied to one numeric column at a time. This is useful when a vector is stored as a column.
marks = data.frame(
math = c(80, 90, 70),
science = c(75, 85, 95)
)
mean(marks$math)
mean(marks$science)
[1] 80
[1] 85
Here, marks$math and marks$science are numeric vectors extracted from the data frame.
Checklist for Calculating Mean of Vectors in R
- Check that the vector contains numeric or logical values before using
mean(). - Use
na.rm=TRUEonly when missing values should be ignored. - Use
trimonly when extreme values should be excluded from both ends of the sorted vector. - Remember that
TRUEis treated as1andFALSEas0in logical vectors. - When calculating a mean from a data frame, apply
mean()to a numeric column, not to the whole data frame.
FAQs on mean() and Vectors in R
What does the mean() function do in R?
The mean() function calculates the arithmetic average of a numeric or logical vector. For numeric values, it adds the values and divides by the number of values used in the calculation.
How do you find the mean value of a vector in R?
Pass the vector to mean(). For example, mean(c(5, 10, 15)) returns 10.
Why is mean() returning NA in R?
mean() returns NA when the input contains NA and na.rm is left as FALSE. Use mean(x, na.rm = TRUE) to remove missing values before calculating the result.
How do you calculate a trimmed mean of a vector in R?
Use the trim argument. For example, mean(x, trim = 0.1) removes 10% of the values from the lower end and 10% from the upper end after sorting, then calculates the average of the remaining values.
Can mean() be used on a logical vector in R?
Yes. In a logical vector, TRUE is treated as 1 and FALSE is treated as 0. So mean() gives the proportion of TRUE values.
Conclusion
In this R Tutorial, we have learnt about mean() function and how to calculate the mean of a vector in R with numeric, logical, trimmed, and missing-value examples.
TutorialKart.com