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


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

Click to see solution
[1] 1.666667

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

Click to see solution

See our mean page for information on na.rm.

var(c(1,2,3,NaN), na.rm=TRUE)
[1] 1
var(c(1,2,3,NA), na.rm=TRUE)
[1] 1
var(c(1,2,NA,NaN,4), na.rm=TRUE)
[1] 2.333333

How do I get the standard deviation of a vector of values, without using sd?

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=TRUE))
[1] 1
sqrt(var(c(1,2,3,NA), na.rm=TRUE))
[1] 1
sqrt(var(c(1,2,NA,NaN,4), na.rm=TRUE))
[1] 1.527525