var

var is a function that calculates the variance of a vector of values.

Examples

How do I get the variance of a vector of values?

Click to see solution
var(c(1,2,3,4))
[1] 1.666667

How do I get the variance of the values in a vector when some of the values are: NA, NaN?

Click to see solution
var(c(1,2,3,NaN), na.rm=T)
[1] 1
var(c(1,2,3,NA), na.rm=T)
[1] 1
var(c(1,2,NA,NaN,4), na.rm=T)
[1] 2.333333

How do I get the standard deviation of a vector of values?

Click to see solution

The standard deviation is equal to the square root of the variance.

sqrt(var(c(1,2,3,NaN), na.rm=T))
[1] 1
sqrt(var(c(1,2,3,NA), na.rm=T))
[1] 1
sqrt(var(c(1,2,NA,NaN,4), na.rm=T))
[1] 1.527525