var() Function in R – Calculating Variance

Variance measures how a set of observations is dispersed around the mean or the average. It is a numerical value that indicates how widely the data in a dataset varies. The more dispersed the data around the dataset mean, the larger the variance. Mathematically, it is the square of the standard deviation. This guide shows you how to calculate variance using the var() function in R.

Calculating Variance in R

R comes with a built-in function to compute the variance of a vector.

Syntax

``var(x)``

Arguments

• x = numerical vector

Example 1: Calculating the variance of a numerical vector

``````#creating a numeric vector
x <- c(172, 155, 167, 141, 156, 118, 139)
#calculating variance
var(x)``````

Output

``[1] 355.6``

Example 2: Calculating variance from a standard deviation

As mentioned above, variance is the square of the standard deviation. To square the given standard deviation, use the ^ operator.

``````#given standard deviation
std_dev <- 11.8997
#calculating variance
variance <- std_dev^2
variance``````

Output

``[1] 141.6029``