Course
When it comes to summarizing numeric data in R, calculating the average is often the first step. It’s often the first thing I do, anyway.
Now, “average” doesn’t have to refer to the mean, necessarily, but in this article, I’m assuming that it does because I’m going to show you how to work with the mean()
function in R, which is R’s standard tool for this purpose.
What Does mean() Do in R?
The mean()
function computes the arithmetic average of numeric data. In other words, it sums up all the values in your vector or column and divides by the total number of values present. Since mean()
is part of base R, there's no need to load additional packages. So it's ready for use as soon as you open R.
The mean() Function Syntax
So we know what mean()
accomplishes, but let’s look at its basic syntax so you can start applying it:
mean(x, na.rm = FALSE, ...)
-
x
: A numeric vector or object. -
na.rm
: Logical. IfTRUE
,mean()
will ignoreNA
values.
As we go forward, know that handling missing data and working with different data structures does introduce some nuances, so keep an eye out.
Calculating the Mean of a Numeric Vector in R
Probably the most basic thing is to calculate the mean of a vector:
daily_temperatures <- c(72, 68, 75, 70, 69)
mean(daily_temperatures)
This line returns the average of 72, 68, 75, 70, and 69, which is 70.8.
Handling Missing Values with na.rm in R
Real datasets have missing values. If your data contains any missing items, mean()
will, as a default behavior, return NA
. This might surprise you if you’re not expecting it, which is why we are taking the time to learn about the na.rm
argument.
survey_scores <- c(82, 90, NA, 88, 85)
mean(survey_scores)
Running this will return NA
. To instruct R to ignore missing values, just set na.rm = TRUE
:
mean(survey_scores, na.rm = TRUE)
Now, mean()
calculates the average using only the available numbers.
Using mean() with Data Frames in R
Let’s extend its use to data frames, which are a typical structure for datasets in R. If you want the mean of a particular column, simply refer to it using the $
operator:
student_scores <- data.frame(
student_name = c("Amir", "Bianca", "Carlos"),
math_score = c(92, 85, 88)
)
mean(student_scores$math_score)
This calculates the average score for all students in your data frame. You can apply this approach to any numeric column (but it has to be a numeric column).
Mean of Logical Values in R
So far, we’ve focused on numeric data, but you might be surprised to learn that mean()
also works with logical vectors. This is a small but important detail I glossed over so far.
You can use mean()
with logical values because, in R, TRUE
is treated as 1
and FALSE
as 0
, so taking the mean of a logical vector will return the proportion of TRUE
values. This can be helpful if you have to summarize binary outcomes.
quiz_passed_flags <- c(TRUE, FALSE, TRUE, TRUE)
mean(quiz_passed_flags)
Here, the result is 0.75, since 3 out of 4 values are TRUE
.
Finding the Weighted Mean in R
Sometimes you need a weighted average, when you are thinking that different values contribute unequally to the result. Enter weighted.mean()
:
course_grades <- c(88, 92, 75)
assignment_weights <- c(0.3, 0.4, 0.3)
weighted.mean(course_grades, assignment_weights)
By using weighted.mean()
, you’re able to account for varying importance or frequency among your values. mean()
won’t do this on its own.
Some Things to Watch Out For
I’ve mentioned most of these things so far, but let me jot down possible issues or notes all in one place:
-
Non-numeric data: If your vector or column isn’t numeric or logical,
mean()
will throw an error. -
NA handling: Forgetting to use
na.rm = TRUE
when you have missing data will trip you up every time. -
Empty vectors:
mean(numeric(0))
returnsNA
with a warning.
Similar Functions in R
R provides several related functions that help you explore your data from different angles. All of these are also in base R, meaning you can use these functions without importing libraries.
-
median()
: For the median value -
sd()
: For standard deviation -
sum()
: For the sum of values
If you want to learn more about the importance of different measures of center, check out our very complete tutorial, Mean vs. Median: Knowing the Difference.
Conclusion
In most basic data analysis tasks, mean()
is your first stop for summarizing numbers.
Keep learning with our courses, such as Introduction to R and Exploratory Data Analysis in R.

I'm a data science writer and editor with contributions to research articles in scientific journals. I'm especially interested in linear algebra, statistics, R, and the like. I also play a fair amount of chess!