Written and maintained by CASRAI Editorial Board
Published · Last updated
On this page
The sd() function in R returns the sample standard deviation of a numeric vector: it divides the summed squared deviations by n − 1, not n. Its full signature is sd(x, na.rm = FALSE), and it ships in the stats package that R loads by default. This guide is for researchers and analysts who need to run it correctly, check the number by hand, and handle missing values.
This guide is the quick-reference companion to Standard Deviation in R, which covers the wider workflow; this page concentrates on the sd() signature, a hand-checked n − 1 example, rescaling to a population figure and the edge cases that return NA.
Ask CASRAI · free to tryQuestion about this guide? Ask CASRAI answers with linked sources. 2 free questions a day, no account.
What sd() is used for in research
Standard deviation summarises how far observations typically sit from their mean, and it is the spread figure reported alongside a mean in most results tables. In R, sd() is the standard route to that figure for one numeric variable. The R reference describes it as computing the standard deviation of the values in x, and states that, like var, it uses the denominator n − 1 (R Core Team, stats::sd reference page, read 2026-11-08). That makes it an estimator of the spread in a population from a sample, which is what most empirical studies need. For the broader language, see What Is R? The Statistical Programming Language Explained.
Install or access (official source only)
Nothing extra is needed. R is distributed from the Comprehensive R Archive Network at cran.r-project.org, and sd belongs to the stats package, which is attached automatically when R starts. You do not call library() first. Installation and administration are documented in the manuals listed at cran.r-project.org/manuals.html.
Step-by-step: compute a standard deviation
- Put your values in a numeric vector, for example
x = c(2, 4, 4, 4, 5, 5, 7, 9). - Call the function:
sd(x). - If the vector contains missing values (
NA), decide whether to drop them and, if so, callsd(x, na.rm = TRUE). - Report the value together with the sample size, and state that it is the sample standard deviation (n − 1 denominator).
The documented arguments are:
| Argument | Meaning (per the R reference) | Default |
|---|---|---|
x |
A numeric vector, or an R object (but not a factor) coercible to numeric by as.double(x) |
none |
na.rm |
Logical: should missing values be removed before computing? | FALSE |
Output and how to read it: a worked hand-check
The value sd() returns is the square root of the sum of squared deviations from the mean divided by n − 1. You can verify it by arithmetic, which is the quickest way to confirm which denominator R used. Take the vector (2, 4, 4, 4, 5, 5, 7, 9):
- The mean is 40 / 8 = 5.
- The squared deviations are 9, 1, 1, 1, 0, 0, 4, 16, which sum to 32.
- Divide by n − 1 = 7: 32 / 7 ≈ 4.5714 (this is the variance, what
var(x)returns). - Take the square root: √4.5714 ≈ 2.138, so
sd(x)is approximately 2.138.
Had the denominator been n = 8, the answer would be √(32 / 8) = 2 exactly. R does not return 2, because sd() uses n − 1. The R reference’s own example makes the same point on a smaller vector: sd(1:2)^2 equals var(1:2), which is 0.5 (deviations of ±0.5 from the mean 1.5 give a sum of squares of 0.5, divided by 1).
Population standard deviation
R has no separate population-standard-deviation function in stats. If your design genuinely treats the data as the entire population, rescale the result by arithmetic: sd(x) * sqrt((length(x) - 1) / length(x)). For the vector above this gives 2. For most sampled data, the n − 1 version that sd() returns is the one to report.
Troubleshooting
| Situation | What happens | What to do |
|---|---|---|
Vector contains NA and na.rm is left at its default FALSE |
Missing values are not removed, so you do not get a usable number back (the result is NA) |
Use sd(x, na.rm = TRUE) once you have decided that dropping them is appropriate for your analysis |
| Vector has length one or length zero | The R reference states the result is NA |
Check that your subsetting returned at least two values |
x is a factor |
The reference excludes factors from the accepted input | Convert deliberately to a numeric variable if the underlying values are numbers, rather than relying on the integer codes of the levels |
| Result differs from a spreadsheet or another package | Check the denominator: sd() uses n − 1 |
Confirm the other tool’s setting (sample versus population) before concluding that one is wrong |
Note that na.rm = TRUE changes n as well as the values. Report the number of observations actually used, not the number collected.
sd, var and mad: which function when
| Function | Relationship to sd() per the R reference |
|---|---|
sd(x) |
Sample standard deviation, n − 1 denominator |
var(x) |
The square of the standard deviation; uses the same n − 1 denominator |
mad(x) |
Named in the reference’s See also section as the most robust alternative |
If you need a rough check on whether a standard deviation is plausible, the range rule of thumb estimates it from the range.
Free alternatives
Each of these is free and open source, and each documents its own default denominator. Defaults differ, so check before comparing numbers with R.
| Tool | Function | Default denominator | Official documentation |
|---|---|---|---|
| R | sd(x, na.rm = FALSE) |
n − 1 | stats::sd |
| Python (NumPy) | numpy.std(a, ddof=0) |
n (ddof=0); pass ddof=1 for n − 1 |
numpy.std |
| Python (pandas) | Series.std(ddof=1) |
n − 1 (ddof=1) |
pandas.Series.std |
| Julia | Statistics.std(itr; corrected=true) |
n − 1 (corrected=true) |
Statistics.std |
The NumPy default is the one most likely to surprise an R user: numpy.std(x) returns the population figure unless you set ddof=1.
Commercial tools: CASRAI guides
| Tool | Guide on CASRAI |
|---|---|
| MATLAB | Standard Deviation in MATLAB: std Function Guide |
| SAS | STD Function in SAS: Standard Deviation in SAS Explained |
| Python (pandas, grouped aggregation) | pandas agg: groupby().agg(), Named Aggregation and Examples |
Check each tool’s own documentation for its default denominator before comparing numbers across packages.
FAQ
Does sd() in R divide by n or n − 1?
By n − 1. The R reference states that, like var, it uses denominator n − 1, so it returns the sample standard deviation.
How do I ignore missing values in sd()?
Set na.rm = TRUE: sd(x, na.rm = TRUE). The default is FALSE, so missing values are kept unless you ask for them to be removed.
What does sd() return for a single value?
NA. The reference states the standard deviation of a length-one or zero-length vector is NA.
How do I get the population standard deviation in R?
Multiply the result by sqrt((n - 1) / n), where n is the number of values used, as shown in the worked example above.
What is the relationship between sd() and var()?
var() returns the variance, the square of the standard deviation. The reference’s own example, sd(1:2)^2, equals 0.5, the same as var(1:2).
Sources
- R Core Team. Standard Deviation: stats::sd reference page, R documentation, read 2026-11-08.
- NumPy, pandas and Julia documentation pages linked in the Free alternatives table, read 2026-11-08.
- R Project. CRAN manuals and CRAN download page, read 2026-11-08.







