How to Calculate Standard Error in R

You can easily calculate the standard error of the mean using functions contained within the base R package. Use the SD function (standard deviation in R) for standalone computations.

# Calculate Standard Error in R
> product_tests <- c(15,13,12,35,12,12,11,13,12,13,15,11,13,12,15)

# Calculate Standard Error in R 
# using the SD function / SQRT of vector length

> sd(product_tests)/sqrt(length(product_tests))
[1] 1.519607

One annoying quirk of real life data sets is they often have missing values. You can use the na.rm option and na.omit function as noted below (for the standard deviation in r function) to clean up the missing values and calculate the standard error using only the real values of the series.

# Calculate Standard Error in R

> product_tests <- c(15,13,12,35,12,12,11,13,12,13,15,11,13,12,15, NA, NA, NA)

> product_tests
 [1] 15 13 12 35 12 12 11 13 12 13 15 11 13 12 15 NA NA NA

> sd(product_tests, na.rm=TRUE)/sqrt(length(na.omit(product_tests)))
[1] 1.519607

Related Materials