Skip to content
Related Articles

Related Articles

How to Find Standard Deviation in R?
  • Last Updated : 07 Apr, 2021

In this article, we will discuss how to find the Standard Deviation in R Programming Language. Standard deviation is the measure of the dispersion of the values. It can also be defined as the square root of variance.

Formula of sample standard deviation:

s = \sqrt{\frac{1}{N-1}\displaystyle\sum\limits_{i=1}^N(x_i-\overline{x})^2 }

where, 

  • s = sample standard deviation
  • N = Number of entities
  • \overline{x} = Mean of entities

Basically, there are two different ways to calculate standard Deviation in R Programming language, both of them are discussed below.



Method 1: Naive approach

In this method of calculating the standard deviation, we will be using the above standard formula of the sample standard deviation in R language. 

Example 1:

R




v <- c(12,24,74,32,14,29,84,56,67,41)
  
s<-sqrt(sum((v-mean(v))^2/(length(v)-1)))
  
print(s)

Output:

[1] 25.53886

Example 2:

R




v <- c(1.8,3.7,9.2,4.7,6.1,2.8,6.1,2.2,1.4,7.9)
  
s<-sqrt(sum((v-mean(v))^2/(length(v)-1)))
  
print(s)

Output:



[1] 2.676004

Method 2: Using sd()

The sd() function is used to return the standard deviation.

Syntax: sd(x, na.rm = FALSE)

Parameters:

  • x: a numeric vector, matrix or data frame.
  • na.rm: missing values be removed?

Return: The sample standard deviation of x.

Example 1:

R




v <- c(12,24,74,32,14,29,84,56,67,41)
  
s<-sd(v)
  
print(s)

Output:

[1] 25.53886

Example 2:

R




v <- c(71,48,98,65,45,27,39,61,50,24,17)
  
s1<-sqrt(sum((v-mean(v))^2/(length(v)-1)))
print(s1)
  
s2<-sd(v)
print(s2)

Output:

[1] 23.52175

Example 3:

R




v <- c(1.8,3.7,9.2,4.7,6.1,2.8,6.1,2.2,1.4,7.9)
  
s1<-sqrt(sum((v-mean(v))^2/(length(v)-1)))
print(s1)
  
s2<-sd(v)
print(s2)

Output:

[1] 2.676004

My Personal Notes arrow_drop_up
Recommended Articles
Page :