Today, we will learn about how to use the var() function in the R programming language. If you want to learn more about this topic, so please read the full this article below.
What is the var() function in R?
The var() function in the R programming language calculates a vector’s sample variance. A measure of variability is called Variance. The average squared departures from the mean are used to determine the Variance. The Variance reveals how widely distributed your dataset is. The Variance around the mean increases with the degree of data separation. Let’s look at the syntax and code example below.
Syntax:
var(x, na.rm = FALSE)
Parameters:
- x: The vector, matrix, …
- na.rm: The default leaving NA values in place is false, and removing them is true.
How to use var() function in R?
var() function: Calculating Variance of a vector
Here, we can create a vector, and we will calculate the Variance of this vector.
# Create a vector x <- c(6,9,59,4,8,2,3,4,-8,9,6,4) # Apply var function in R res <- var(x) cat('The var is:',res)
Output
The var is: 269.7879
var() function: Calculating Variance of a data frame
As a vector, we can calculate the Variance of a data frame with specific columns in this data frame. Follow the code example below.
First, we will create a data frame as follows:
# Create a data frame df <- data.frame( ID = c(1,2,3,4,5,6,7,8), Math = c(6,8,9,6,3,2,4,9), Physics = c(8,9,6,4,5,9,7,6), English = c(9,5,6,3,2,1,4,8) ) # View data frame df
Output
ID Math Physics English
1 1 6 8 9
2 2 8 9 5
3 3 9 6 6
4 4 6 4 3
5 5 3 5 2
6 6 2 9 1
7 7 4 7 4
8 8 9 6 8
Then we can calculate variance as follows:
# Create a data frame df <- data.frame( ID = c(1,2,3,4,5,6,7,8), Math = c(6,8,9,6,3,2,4,9), Physics = c(8,9,6,4,5,9,7,6), English = c(9,5,6,3,2,1,4,8) ) # Calculate variance resMath <- var(df$Math) resPhysics <- var(df$Physics) resEnglish <- var(df$English) # View variance cat('The var of Math is:',resMath,'\n') cat('The var of Physics is:',resPhysics,'\n') cat('The var of English is:',resEnglish)
Output
The var of Math is: 7.267857
The var of Physics is: 3.357143
The var of English is: 7.928571
var() function: Converting Variance to Standard Deviation
As you have learned, the Variance is equal to the square of the standard deviation. Thus we can convert the Variance to the standard deviation by the square root of the method. See the code below:
# Create a vector x <- c(6,8,9,6,3,2,4,9) # Calculate the standard deviation through the variance res <- sqrt(var(x)) # View result cat('Convert Variance to Standard Deviation:', res)
Output
Convert Variance to Standard Deviation: 2.695896
Here, you can check result by using sd() function in R.
# Create a vector x <- c(6,8,9,6,3,2,4,9) # Calculate the standard deviation through the variance res <- sqrt(var(x)) # Calculate the standard deviation using sd() function res1 <- sd(x) # View result cat('Convert Variance to Standard Deviation:', res,'\n') cat('The standard deviation', res1)
Output
Convert Variance to Standard Deviation: 2.695896
The standard deviation 2.695896
Summary
This article shares calculated Variance by using the var() function in R language. So, if you have any questions, please comment below.
Have a great day!
Maybe you are interested:
- sub() and gsub() function in R
- The cut() Function In R: How To Use cut() In R
- The lines Function In R: Adding Lines To A Plot

Hi, guys! My name’s Scott Miller. My current job is a software developer and I have shared a lot of quality articles about Javascript, C, C++, C#, Python, PHP, R, Java programming languages. I’m hoping they can assist you.
Name of the university: HCMUS
Major: IT
Programming Languages: C, C++, Python, R, Java, JavaScript