Variability in R Programming
Last Updated :
25 Apr, 2025
Variability (also known as Statistical Dispersion) is another feature of descriptive statistics. Measures of central tendency and variability together comprise of descriptive statistics. Variability shows the spread of a data set around a point. Example: Suppose, there exist 2 data sets with the same mean value:
A = 4, 4, 5, 6, 6 Mean(A) = 5 B = 1, 1, 5, 9, 9 Mean(B) = 5
So, to differentiate among the two data sets, R offers various measures of variability.
Measures of Variability
Following are some of the measures of variability that R offers to differentiate between data sets:
Variance
Variance is a measure that shows how far each value is from a particular point, preferably the mean value. Mathematically, it is defined as the average of squared differences from the mean value. Formula: [Tex]\displaystyle \sigma^2 = \frac{\displaystyle\sum_{i=1}^{n}(x_i – \mu)^2} {n} [/Tex]where,
[Tex] [/Tex]specifies variance of the data set [Tex] [/Tex]specifies [Tex]i^{\text{th}} [/Tex]value in data set [Tex] [/Tex]specifies the mean of data set n specifies total number of observations
In the R language, there is a standard built-in function to calculate the variance of a data set.
Syntax: var(x) Parameter: x: It is data vector
Example:
R
x <- c (5, 5, 8, 12, 15, 16)
print ( var (x))
|
Output:
[1] 23.76667
Standard Deviation
Standard deviation in statistics measures the spreadness of data values with respect to mean and mathematically, is calculated as square root of variance. Formula: [Tex]\displaystyle \sigma = \sqrt{\frac{\displaystyle\sum_{i=1}^{n}(x_i – \mu)^2} {n}} [/Tex]where,
[Tex] [/Tex]specifies standard deviation of the data set [Tex] [/Tex]specifies [Tex]i^{\text{th}} [/Tex]value in data set [Tex] [/Tex]specifies the mean of data set n specifies total number of observations
In R language, there is no standard built-in function to calculate the standard deviation of a data set. So, modifying the code to find the standard deviation of data set. Example:
R
x <- c (5, 5, 8, 12, 15, 16)
d <- sqrt ( var (x))
print (d)
|
Output:
[1] 4.875107
Range
Range is the difference between the maximum and minimum value of a data set. In R language, max() and min() is used to find the same, unlike range() function that returns the minimum and maximum value of the data set.
Example:
R
x <- c (5, 5, 8, 12, 15, 16)
print ( range (x))
print ( max (x)- min (x))
|
Output:
[1] 5 16
[1] 11
Mean Deviation
Mean deviation is a measure calculated by taking an average of the arithmetic mean of the absolute difference of each value from the central value. Central value can be mean, median, or mode. Formula: [Tex]\displaystyle \mathrm{MD} \equiv \frac{1}{n} \sum_{i=1}^{n}\left|x_{i}-\mu\right| [/Tex]where,
[Tex] [/Tex]specifies [Tex]i^{\text{th}} [/Tex]value in data set [Tex] [/Tex]specifies the mean of data set n specifies total number of observations
In R language, there is no standard built-in function to calculate mean deviation. So, modifying the code to find the mean deviation of the data set.
Example:
R
x <- c (5, 5, 8, 12, 15, 16)
md <- sum ( abs (x- mean (x)))/ length (x)
print (md)
|
Output:
[1] 4.166667
Interquartile Range
Interquartile Range is based on splitting a data set into parts called as quartiles. There are 3 quartile values (Q1, Q2, Q3) that divide the whole data set into 4 equal parts. Q2 specifies the median of the whole data set. Mathematically, the interquartile range is depicted as:
IQR = Q3 – Q1
where,
Q3 specifies the median of n largest values Q1 specifies the median of n smallest values
In R language, there is a built-in function to calculate the interquartile range of data set.
Syntax: IQR(x) Parameter: x: It specifies the data set
Example:
R
x <- c (5, 5, 8, 12, 15, 16)
print ( IQR (x))
|
Output:
[1] 8.5
Similar Reads
Reproducibility In R Programming
Reproducibility in R Programming Language refers to the ability to recreate and replicate the results of a data analysis or computational experiment. It ensures that the code, data, and environment are well-documented and organized, allowing others (or even yourself at a later time) to obtain the sa
4 min read
Hello World in R Programming
When we start to learn any programming languages we do follow a tradition to begin HelloWorld as our first basic program. Here we are going to learn that tradition. An interesting thing about R programming is that we can get our things done with very little code. Before we start to learn to code, le
2 min read
Data Reshaping in R Programming
Generally, in R Programming Language, data processing is done by taking data as input from a data frame where the data is organized into rows and columns. Data frames are mostly used since extracting data is much simpler and hence easier. But sometimes we need to reshape the format of the data frame
5 min read
tidyr Package in R Programming
Packages in the R language are a collection of R functions, compiled code, and sample data. They are stored under a directory called âlibraryâ in the R environment. By default, R installs a set of packages during installation. One of the most important packages in R is the tidyr package. The sole pu
14 min read
How to Code in R programming?
R is a powerful programming language and environment for statistical computing and graphics. Whether you're a data scientist, statistician, researcher, or enthusiast, learning R programming opens up a world of possibilities for data analysis, visualization, and modeling. This comprehensive guide aim
4 min read
How To Start Programming With R
R Programming Language is designed specifically for data analysis, visualization, and statistical modeling. Here, we'll walk through the basics of programming with R, from installation to writing our first lines of code, best practices, and much more. Table of Content 1. Installation2. Variables and
12 min read
Learn R Programming
R is a Programming Language that is mostly used for machine learning, data analysis, and statistical computing. It is an interpreted language and is platform independent that means it can be used on platforms like Windows, Linux, and macOS. In this R Language tutorial, we will Learn R Programming La
15+ min read
MANOVA Test in R Programming
Multivariate analysis of variance (MANOVA) is simply an ANOVA (Analysis of variance) with several dependent variables. It is a continuation of the ANOVA. In an ANOVA, we test for statistical differences on one continuous dependent variable by an independent grouping variable. The MANOVA continues th
3 min read
Basic Syntax in R Programming
R is the most popular language used for Statistical Computing and Data Analysis with the support of over 10, 000+ free packages in CRAN repository. Like any other programming language, R has a specific syntax which is important to understand if you want to make use of its powerful features. This art
3 min read
Contingency Tables in R Programming
Prerequisite: Data Structures in R ProgrammingContingency tables are very useful to condense a large number of observations into smaller to make it easier to maintain tables. A contingency table shows the distribution of a variable in the rows and another in its columns. Contingency tables are not o
6 min read