How to Calculate Standard Deviation in R: Simple Guide
In R, you can calculate the standard deviation of a numeric vector using the
sd() function. Simply pass your data vector to sd(), and it returns the standard deviation value.Syntax
The basic syntax to calculate standard deviation in R is:
sd(x, na.rm = FALSE)
Where:
xis a numeric vector of data.na.rmis a logical value indicating whether to removeNA(missing) values before calculation. Default isFALSE.
r
sd(x, na.rm = FALSE)
Example
This example shows how to calculate the standard deviation of a numeric vector in R, including handling missing values.
r
data <- c(10, 12, 23, 23, 16, 23, 21, 16, NA) # Calculate standard deviation without removing NA sd(data) # Calculate standard deviation with NA removed sd(data, na.rm = TRUE)
Output
[1] NA
[1] 5.345224
Common Pitfalls
Common mistakes when calculating standard deviation in R include:
- Not handling
NAvalues, which causessd()to returnNA. - Passing non-numeric data, which results in errors.
- Confusing population standard deviation with sample standard deviation;
sd()calculates sample standard deviation by default.
r
data_with_na <- c(5, 7, NA, 9) # Wrong: ignoring NA leads to NA result sd(data_with_na) # Right: remove NA to get correct result sd(data_with_na, na.rm = TRUE)
Output
[1] NA
[1] 2
Quick Reference
| Function | Description |
|---|---|
| sd(x) | Calculates sample standard deviation of numeric vector x |
| sd(x, na.rm = TRUE) | Calculates standard deviation ignoring missing values |
| var(x) | Calculates variance of numeric vector x (square of sd) |
Key Takeaways
Use the sd() function to calculate standard deviation of numeric data in R.
Set na.rm = TRUE to ignore missing values and avoid NA results.
sd() computes sample standard deviation, not population standard deviation.
Ensure your data vector is numeric to prevent errors.
Use var() if you need variance instead of standard deviation.