Standard Deviation in R

Standard deviation in R, can be calculated using sd() function.

Syntax for standard deviation function in R:

sd(x, na.rm = FALSE, …)
  • x – numeric vector
  • rm- whether NA should be removed, if not, NA will be returned

Example of standard deviation sd() function in R

# standard deviation in R: sd() function

x <-c(1.234,2.342,-4.562,5.671,12.345,-14.567)
sd(x)

output:

[1] 9.203969

 

Example of standard deviation sd() function in R with NA:

sd() function doesn’t give desired output, If NAs are present in the vector. so it has to be handled by using na.rm=TRUE in sd() function

# sd() function in R for input vector which has NA.

x <-c(1.234,2.342,-4.562,5.671,12.345,-14.567,NA)
sd(x,na.rm=TRUE)

output:

[1] 9.203969

 

Example of standard deviation sd() function in R for a column in data frame:

Lets use mtcars data frame to demonstrate standard deviation function sd() in r

# standard deviation in R for a column in data frame

sd(mtcars$mpg)

output:

[1] 6.026948

previous small standard deviation in r                                                                                                           next small standard function in r

Author

  • Sridhar Venkatachalam

    With close to 10 years on Experience in data science and machine learning Have extensively worked on programming languages like R, Python (Pandas), SAS, Pyspark.