Solved – What formula is used for standard deviation in R

rstandard deviation

What formula is used in the standard deviation function sd in R?

Best Answer

As pointed out by @Gschneider, it computes the sample standard deviation

$$\sqrt{\frac{\sum\limits_{i=1}^{n} (x_i - \bar{x})^2}{n-1}}$$

which you can easily check as follows:

> #generate a random vector
> x <- rnorm(n=5, mean=3, sd=1.5)
> n <- length(x)
> 
> #sd in R
> sd1 <- sd(x)
> 
> #self-written sd
> sd2 <- sqrt(sum((x - mean(x))^2) / (n - 1))
>  
> #comparison
> c(sd1, sd2)   #:-)
[1] 0.6054196 0.6054196