Mean and Median in R
Learn how to calculate the mean and median in R with the mean() and median() functions, handle missing values with na.rm, and read summary statistics.
Contents
The mean and median in R come from two base functions, mean() and median(), that each reduce an R vector of numbers to a single summary value. You pass the vector as the argument, and R returns the arithmetic average or the middle value. The two numbers answer different questions about the center of your data, so reading them side by side tells you more than either alone.
Using the mean() function
The mean() function adds the values in an R vector and divides by the count of values. A call such as mean(x) returns the arithmetic average of the numbers stored in x. The result is sensitive to extreme values, because one large number pulls the average toward it. Check the range of your vector before you trust a single average figure.
Using the median() function
The median() function sorts the values of an R vector and returns the middle one. When the vector holds an even number of values, median() averages the two central values instead. The median resists extreme values, so a single outlier moves it far less than it moves the mean. Comparing mean(x) with median(x) shows whether your measurements are skewed.
Handling missing values with na.rm
Missing values in R appear as NA, and both mean() and median() return NA when the vector contains one. The na.rm argument controls this, and na.rm = TRUE removes the missing entries before the calculation. Set na.rm = TRUE only when you know why those values are absent. The summary() function reports several summary statistics at once, including the mean and median, alongside the minimum and maximum.
Questions About Mean and Median in R
How do you calculate mean and median in R?
You calculate mean and median in R by calling mean(x) and median(x) on the same R vector x. Each function returns one number, so assign both results to named objects if you want to keep them. Printing the two values next to each other is a quick check of the center of your data.
Why do mean() and median() return NA?
The mean() and median() functions return NA because the R vector contains at least one missing value. R refuses to compute across an unknown entry unless you tell it how to treat that entry. Add na.rm = TRUE to either call to drop the missing values first.
What does na.rm = TRUE do in mean() and median()?
The na.rm = TRUE argument tells mean() and median() to remove missing values before calculating. The calculation then uses only the entries that hold real numbers. Report how many values were dropped, because the result describes a smaller set than the full vector.
Should you report the mean or the median?
Report the mean when your measurements are roughly symmetric and you want the arithmetic center. Report the median when extreme values would distort the average, as with skewed field counts. Showing both the mean and the median in R gives the reader a fuller picture of the data.