Skip to main content

R mean() Function: Get Started with Averages

Calculate the average of numeric, logical, and weighted data using R’s built-in mean functions. Understand how to handle missing values and apply the function to vectors and data frames.
Jun 19, 2025  · 4 min read

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. If TRUE, mean() will ignore NA 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)) returns NA 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.


Josef Waples's photo
Author
Josef Waples

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! 

Topics

Learn R with DataCamp

Course

Introduction to R

4 hr
2.9M
Master the basics of data analysis in R, including vectors, lists, and data frames, and practice R with real data sets.
See DetailsRight Arrow
Start Course
See MoreRight Arrow
Related

Tutorial

Arithmetic Mean: A Foundational Tool for Data Analysis

Explore the arithmetic mean's role in data analysis. Learn its formula, applications, and how it compares to other kinds of means and other statistical measures, and understand when each is most useful.
Vinod Chugani's photo

Vinod Chugani

7 min

Tutorial

R Formula Tutorial

Discover the R formula and how you can use it in modeling- and graphical functions of well-known packages such as stats, and ggplot2.
Karlijn Willems's photo

Karlijn Willems

15 min

Tutorial

How to Use na.rm to Handle Missing Values in R

We set na.rm = TRUE in common R functions to exclude missing (NA) values. This helps us compute accurate statistics and enhances the reliability of our results.
Vidhi Chugh's photo

Vidhi Chugh

8 min

Tutorial

Operators in R

Learn how to use arithmetic and logical operators in R. These binary operators work on vectors, matrices, and scalars.
DataCamp Team's photo

DataCamp Team

4 min

Tutorial

Rank Formula in Excel: A Comprehensive Guide With Examples

Learn how to rank data in Excel with RANK(), RANK.EQ(), and RANK.AVG() functions. Understand their differences, applications, and tips for accurate data analysis.
Laiba Siddiqui's photo

Laiba Siddiqui

11 min

Tutorial

T-tests in R Tutorial: Learn How to Conduct T-Tests

Determine if there is a significant difference between the means of the two groups using t.test() in R.
Abid Ali Awan's photo

Abid Ali Awan

10 min

See MoreSee More