Related Articles

Related Articles

Calculate the Average, Variance and Standard Deviation in R Programming
  • Last Updated : 25 Aug, 2020

R 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

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

A = (x_1 + x_2 ........ + x_n)/ n

Example:

Suppose there are 8 data points,



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

The average of these 8 data points is,

A = \frac{2 + 4 + 4 + 4 + 5 + 5 + 7 + 9}{8} = 5

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

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


Output:

[1] 5

Example 2:



R

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


Output:

[1] 105.5714

Variance

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

Formula: \sigma^{2}= \frac { \sum_{i=1}^{N} (x_{i}-\mu)^{2}}{N}

where,

\mu \, is\, Mean,

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,

\begin{array}{lll} (2-5)^2 = (-3)^2 = 9 && (5-5)^2 = 0^2 = 0 \\ (4-5)^2 = (-1)^2 = 1 && (5-5)^2 = 0^2 = 0 \\ (4-5)^2 = (-1)^2 = 1 && (7-5)^2 = 2^2 = 4 \\ (4-5)^2 = (-1)^2 = 1 && (9-5)^2 = 4^2 = 16. \\ \end{array}   [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

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


Output:

[1] 4.571429

Example 2:

R

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


Output:

[1] 22666.7

Standard Deviation

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,  

Standard Deviation = \sqrt{ variance }

Example:

Standard Deviation for the above data,

Standard Deviation = \sqrt{ 4 } = 2

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

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


Output:

[1] 2.13809

Example 2:

R

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


Output:

[1] 367.6076
My Personal Notes arrow_drop_up
Recommended Articles
Page :