Calculate Variance of a Vector in R

R comes with a number of built-in functions to compute common descriptive statistics like the mean, median, variance, standard deviation, etc. In this tutorial, we will look at how to get the variance of values in an R vector with the help of some examples.

How to get the variance of values in a vector in R?

You can use the R `var()` function to get the variance of values in a vector. Pass the vector as an argument to the function. The following is the syntax –

```# variance of values in a vector
var(x, na.rm=FALSE)```

The following are the arguments that you can give to the `var()` function in R.

• x – The vector for which you want to compute the variance.
• na.rm – (Optional argument) Indicates whether to remove missing values before computing the variance. It is `FALSE` by default.

The function returns the sample variance of values in the passed vector.

If you want to get the population variance instead (considering vector values as population values) multiply the result from the `var()` function by `(n-1)/n`.

Examples

Let’s look at some examples of using the above method to get the variance of a vector.

Variance of values in a numeric vector

Let’s create a vector of numbers (and without any `NA` values) and apply the `var()` function.

```# create a vector
vec <- c(1, 2, 3, 4, 5)
# variance of values in the vector
var(vec)```

Output:

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

`2.5`

We get the sample variance of the values in the above vector as 2.5. If you want to get the population variance, multiply this result by `(n-1)/n`

```# population variance
n <- length(vec)
var(vec) * (n-1)/n```

Output:

`2`

The population variance of the above vector is 2.

Variance of values in a vector with `NA` values

What would happen if there are some `NA` present values in the vector?

Let’s find out.

First, we will create a vector with some `NA` values and then apply the `var()` function without any additional arguments.

```# create a vector with NA values
vec <- c(1, 2, NA, 3, 4, 5, NA)
# variance of values in the vector
var(vec)```

Output:

`<NA>`

You can see that we get `NA` as the output. This is because performing an arithmetic operation with `NA` results in an `NA` in R.

You can pass `TRUE` to the `na.rm` parameter of the `var()` function to exclude missing values when computing the variance of a vector.

```# create a vector with NA values
vec <- c(1, 2, NA, 3, 4, 5, NA)
# variance of values in the vector
var(vec, na.rm = TRUE)```

Output:

`2.5`

Now we get the variance of the values in the above vector as 2.5.

Standard deviation of values in a Vector

Standard deviation is defined as the square root of the variance. You can use the `sqrt()` function in R on the result of the `var()` function to get the standard deviation of a vector. Let’s look at an example.

```# create a vector
vec <- c(1, 2, 3, 4, 5)
# standard deviation of values in the vector
sqrt(var(vec))```

Output:

`1.58113883008419`

Here, we get the standard deviation of the values in the above vector as 1.5811, which is the square root of the variance, 2.5. Alternatively, you can also use the `sd()` function in R to compute the standard deviation directory without using the `var()` function.

You might also be interested in –