# Calculate the Average, Variance and Standard Deviation in R Programming

• Last Updated : 16 Dec, 2021

R Programming Language is an open-source programming language that is widely used as a statistical software and data analysis tool. R generally comes with the Command-line interface. R is available across widely used platforms like Windows, Linux, and macOS. R language provides very easy methods to calculate the average, variance, and standard deviation.

## Average in R Programming

Average a number expressing the central or typical value in a set of data, in particular the mode, median, or (most commonly) the mean, which is calculated by dividing the sum of the values in the set by their number. The basic formula for the average of n numbers x1, x2, ……xn is

Example:

Suppose there are 8 data points,

2, 4, 4, 4, 5, 5, 7, 9

The average of these 8 data points is,

### Computing Average in R Programming

To compute the average of values, R provides a pre-defined function mean(). This function takes a Numerical Vector as an argument and results in the average/mean of that Vector.

Syntax: mean(x, na.rm)

Parameters:

• x: Numeric Vector
• na.rm: Boolean value to ignore NA value

Example 1:

## R

 # R program to get average of a list   # Taking a list of elements list = c(2, 4, 4, 4, 5, 5, 7, 9)   # Calculating average using mean() print(mean(list))

Output:

[1] 5

Example 2:

## R

 # R program to get average of a list   # Taking a list of elements list = c(2, 40, 2, 502, 177, 7, 9)   # Calculating average using mean() print(mean(list))

Output:

[1] 105.5714

## Variance in R Programming Language

Variance is the sum of squares of differences between all numbers and means. The mathematical formula for variance is as follows,

where,

N is the total number of elements or frequency of distribution.

Example:

Let’s consider the same dataset that we have taken in average. First, calculate the deviations of each data point from the mean, and square the result of each,
[Tex]variance = \frac{9 + 1 + 1 + 1 + 0 + 0 + 4 + 16}{8} = 4[/Tex]

#### Computing Variance in R Programming

One can calculate the variance by using var() function in R.

Syntax: var(x)

Parameters:

x: numeric vector

Example 1:

## R

 # R program to get variance of a list   # Taking a list of elements list = c(2, 4, 4, 4, 5, 5, 7, 9)   # Calculating variance using var() print(var(list))

Output:

[1] 4.571429

Example 2:

## R

 # R program to get variance of a list   # Taking a list of elements list = c(212, 231, 234, 564, 235)   # Calculating variance using var() print(var(list))

Output:

[1] 22666.7

## Standard Deviation in R Programming Language

Standard Deviation is the square root of variance. It is a measure of the extent to which data varies from the mean. The mathematical formula for calculating standard deviation is as follows,

Example:

Standard Deviation for the above data,

### Computing Standard Deviation in R

One can calculate the standard deviation by using sd() function in R.

Syntax: sd(x)

Parameters:

x: numeric vector

Example 1:

## R

 # R program to get  # standard deviation of a list   # Taking a list of elements list = c(2, 4, 4, 4, 5, 5, 7, 9)   # Calculating standard  # deviation using sd() print(sd(list))

Output:

[1] 2.13809

Example 2:

## R

 # R program to get  # standard deviation of a list   # Taking a list of elements list = c(290, 124, 127, 899)   # Calculating standard  # deviation using sd() print(sd(list))

Output:

[1] 367.6076

My Personal Notes arrow_drop_up
Recommended Articles
Page :