Mean and Median
The two most-used summaries in all of statistics are one function call each:
mean() adds everything up and divides by the count. median() sorts the values and picks the middle one (or averages the middle two when the count is even). For the 32 cars in mtcars, the mean fuel economy is about 20.1 mpg and the median is 19.2 - close together, which tells you the data isn't badly skewed. When they disagree a lot, that's information too; we'll get to it at the end.
One thing that surprises everyone: if the vector contains even a single NA, both functions return NA:
That's deliberate - R refuses to quietly pretend the missing value isn't there. na.rm = TRUE says "compute on the values you have." Nearly every summary function in this article accepts it. The full story of how NA propagates is in missing values.
Standard Deviation and Variance
sd() measures spread: roughly, how far a typical value sits from the mean, in the same units as the data. var() is its square:
A standard deviation of about 6 mpg means cars typically sit within roughly 6 mpg of the 20.1 average. Because sd() is in the data's own units, it's the one you report; var() mostly shows up inside other formulas.
Here's the detail that matters for coursework: sd() and var() compute the sample statistic - they divide the summed squared deviations by n − 1, not by n:
Why n − 1? Because you estimated the mean from the same data, the deviations around that estimated mean are systematically a bit too small; dividing by n − 1 corrects for it. Since your data is almost always a sample from something bigger, the n − 1 version is what you want. If you genuinely have the entire population (every student in the class, every product in the catalog), multiply: var(x) * (n - 1) / n.
Standard Error of the Mean
Standard deviation and standard error get confused constantly, so keep them apart: sd describes the data, SE describes your estimate of the mean. R has no built-in se(), but the formula is one line:
The standard error shrinks as the sample grows - collect four times the data and the SE halves - because a bigger sample pins down the mean more precisely. The sd does not shrink with sample size; the cars are as varied as they are no matter how many you measure. The SE is the building block of confidence intervals, which is where it earns its keep.
summary() - the One-Call Overview
summary() gives you the five-number summary plus the mean in one shot, and it works on whole data frames:
Called on a data frame, it summarizes every column - numeric columns get min/quartiles/mean/max, and factors get counts per level. It's the first thing to run on any dataset you've just loaded: impossible values (a negative age, a max of 9999) jump out immediately.
Quantiles, Range and IQR
quantile() generalizes the median to any cut point:
With no arguments it returns the minimum, quartiles, and maximum. Pass probs = for specific cut points - the 10th and 90th percentiles above bracket where the bulk of the data lives. IQR() (the distance between the 25th and 75th percentiles) is a spread measure that, unlike sd(), doesn't get dragged around by outliers. range() returns the min and max as a pair.
The Mode: R's mode() Does NOT Do This
This one catches everybody exactly once. R has a function called mode(), and it has nothing to do with statistics - it reports an object's storage type:
The idiom to remember: table(x) counts how often each value appears, which.max() finds the biggest count, and names() pulls out the value itself. Note it comes back as a character string (table names always are); wrap it in as.numeric() if you need to compute with it. If two values tie, which.max() silently returns only the first - check the table yourself when ties are plausible.
Mean vs Median: Which One to Report
The mean uses every value, which is its strength and its weakness - a single extreme value drags it. The median only cares about the middle, so outliers barely touch it:
One added value pushes the mean from about 49,300 to over 155,000 - a number that describes nobody in the data - while the median moves only from 48,000 to 49,500. This is why income, house prices, and hospital stays are reported as medians: skewed data with a long tail makes the mean misleading. For roughly symmetric data the two agree and the mean is fine (and statistically more efficient). A quick histogram tells you which situation you're in - and comparing your mean to your median is itself a one-line skew check.
What You Take Away
mean(x)andmedian(x)- addna.rm = TRUEwhen there are missing values.sd(x)andvar(x)compute the sample statistic (then − 1denominator) - which is what you want.- Standard error is
sd(x) / sqrt(length(x))- it measures how well you know the mean, not how spread the data is. summary()on a fresh data frame is the fastest sanity check in R.- The mode is
names(which.max(table(x)))- R'smode()is about storage types. - Skewed data or outliers: report the median. Symmetric data: the mean is fine.
Next up: measuring how two variables move together - correlation with cor() and cor.test().
Frequently Asked Questions
How do you calculate standard deviation in R?
With sd(x). Note that it computes the sample standard deviation - it divides by n − 1, not n. That's what you want in almost every real analysis, because your data is almost always a sample rather than the entire population.
How do you find the mean and median in R?
mean(x) and median(x). If the vector contains missing values, both return NA - add na.rm = TRUE to compute on the values that are present: mean(x, na.rm = TRUE).
How do you find the mode in R?
Not with mode() - that function returns the storage type of an object, not the most frequent value. Use the table idiom instead: names(which.max(table(x))) returns the value that appears most often.
What is the standard error in R?
There's no built-in function. Compute it as sd(x) / sqrt(length(x)). The standard deviation describes the spread of your data; the standard error describes how precisely you've estimated the mean, and it shrinks as the sample grows.