The variance is a standard of variability. The variance is calculated by taking the average squared deviations from the mean. The variance shows you the degree of spread in your dataset. The more separated the data, the larger the variance is about the mean. Let’s see the definition of variance.

**What is Variance**

The variance is determined as an average of the squared differences from the mean. Variance is a numerical value that shows how widely the individual figures in a data set distribute themselves about the mean.

To calculate the variance in mathematics,

- First, calculate the mean, which is an average of the numbers.

- Second, for each number: subtract the Mean and square the result (the squared difference).

- In the last step, check out the average of those squared differences.

**How to Calculate Variance in R**

To **calculate** the **variance** in **R**, use the **var() **function. The var() is a built-in function that computes the sample variance of a vector. It measures how much value is away from the mean value.

**Syntax**

`var(x, y=NULL, na.rm=FALSE, use)`

**Parameters**

**x,y**

It is a complex vector or matrix.

**na.rm**

Boolean with default **FALSE** meaning to leave NA values present and **TRUE** meaning to remove them.

**use**

Ignored

**Calculate the variance of a numeric vector in R**

To create a numeric vector, use the c() function and pass the multiple numeric arguments. Then gives that numeric vector the var() function, which returns the variance of a vector.

```
weights <- c(60, 55, 50, 65, 59)
var(weights)
```

**Output**

`[1] 31.7`

The var() function calculates the estimated variance (with N–1 in the denominator). To calculate that first variance with N in the denominator, you must multiply this number by (N–1)/N.

**Calculate the Variance of the dataset in R**

We will use a built-in dataset **iris** in this example. To use the built-in dataset, write data(dataset name) at the start of your file. Then, you can use that dataset.

`data(iris)`

We will find the variance of the **petal.length** of the iris dataset.

```
data(iris)
ln <- iris$Petal.Length
var(ln)
```

**Output**

`[1] 3.116278`

The variance of the petal.length is 3.116278.

**Sample Variance vs. Population Variance**

The **main** **difference** between a **sample** and **population** **variance** relates to a variance calculation. **Population variance** refers to the value of **variance** calculated from population data, and **sample** **variance** is the **variance** calculated from **sample** **data**.

The correction does not matter for large sample sizes. However, it does matter when the dataset is small sample sizes. When the variance is calculated from population data, n equals the number of elements.

To calculate the population variance, use the following function.

`mean((x - mean(x)) ^ 2)`

Let’s see how to calculate population variance in R.

```
population_variance <- function(rv) {
mean((rv - mean(rv)) ^ 2)
}
weights <- c(60, 55, 50, 65, 59)
population_variance(weights)
```

**Output**

`[1] 25.36`

**Conclusion**

The var R function computes the sample variance of a numeric input vector. The var() method calculates the sample variance, not the population variance. We have already seen how to calculate population variance.

**See Also**

Krunal Lathiya is an Information Technology Engineer by education and web developer by profession. He has worked with many back-end platforms, including Node.js, PHP, and Python. In addition, Krunal has excellent knowledge of Data Science and Machine Learning, and he is an expert in R Language. Krunal has written many programming blogs, which showcases his vast expertise in this field.