You can use the var function to calculate the sample variance in R. This is part of the base R package, so you don’t need to load additional libraries.
# calculate variance in R > test <- c(41,34,39,34,34,32,37,32,43,43,24,32) > var(test)  30.26515
A common problem with sample data in R is missing values. As the code below indicates, missing values will cause the calculation to crash. You can use the na.rm option contained within the var function to remove missing values. It will calculate the variance using the non-missing values.
# calculate variance in R - missing values example > test <- c(41,34,39,34,34,32,37,32,43,43,24,32, NA,NA) # calculate variance in R - test fails due to NA values > var(test)  NA # calculate variance in R; remove missing values, correct result > var(test, na.rm=TRUE)  30.26515
- Find the mean in R
- Calculate Standard Error in R
- Calculate Standard Deviation in R
- Calculate Variance in R
- Calculate Skewness in R
- Calculate Kurtosis in R
- Calculate Confidence Interval in R
- Using a Chi Square Test in R
- Power analysis in R
- Percentile in R
- Quartile in R