Variance and Standard Deviation


Range

The range describes the difference between the largest and smallest data point in our data set. The bigger the range, the more is the spread of data and vice versa.


Range = Largest data value – smallest data value
# R program to illustrate
# Descriptive Analysis

# Import the data using read.csv()
myData = read.csv("CardioGoodFitness.csv",
stringsAsFactors = F)

# Calculate the maximum
max = max(myData$Age)
# Calculate the minimum
min = min(myData$Age)
# Calculate the range
range = max - min

cat("Range is:\n")
print(range)

# Alternate method to get min and max
r = range(myData$Age)
print(r)

Output:
Range is: 
[1] 32 
[1] 18 50

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.
Computing Variance in R Programming
One can calculate the variance by using var() function in R.

Syntax: var(x)

Parameters:
x: numeric vector
Example:
# 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

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

Comments

Popular posts from this blog

Programming in R - Dr Binu V P

Introduction

R Data Types