This article shows how to calculate Mean, Median, Mode, Variance, and Standard Deviation of any data set using R programming language.
Mean: Calculate sum of all the values and divide it with the total number of values in the data set.
> x <- c(1,2,3,4,5,1,2,3,1,2,4,5,2,3,1,1,2,3,5,6) # our data set
> mean.result = mean(x) # calculate mean
> print (mean.result)
[1] 2.8
Median: The middle value of the data set.
> x <- c(1,2,3,4,5,1,2,3,1,2,4,5,2,3,1,1,2,3,5,6) # our data set
> median.result = median(x) # calculate median
> print (median.result)
[1] 2.5
Mode: The most occurring number in the data set. For calculating mode, there is no default function in R. So, we have to create our own custom function.
> mode <- function(x) {
+ ux <- unique(x)
+ ux[which.max(tabulate(match(x, ux)))]
+ }
> x <- c(1,2,3,4,5,1,2,3,1,2,4,5,2,3,1,1,2,3,5,6) # our data set
> mode.result = mode(x) # calculate mode (with our custom function named ‘mode’)
> print (mode.result)
[1] 1
Variance: How far a set of data values are spread out from their mean.
> variance.result = var(x) # calculate variance
> print (variance.result)
[1] 2.484211
Standard Deviation: A measure that is used to quantify the amount of variation or dispersion of a set of data values.
> sd.result = sqrt(var(x)) # calculate standard deviation
> print (sd.result)
[1] 1.576138
Hope this helps. Thanks.