*24*

You can use the following syntax to calculate the standard deviation of a vector in R:

sd(x)

Note that this formula calculates the sample standard deviation using the following formula:

√Σ (x_{i} – μ)^{2}/ (n-1)

where:

**Σ**: A fancy symbol that means “sum”**x**: The i_{i}^{th}value in the dataset**μ**: The mean value of the dataset**n:**The sample size

The following examples show how to use this function in practice.

**Example 1: Calculate Standard Deviation of Vector**

The following code shows how to calculate the standard deviation of a single vector in R:

#create dataset data #find standard deviation sd(data) [1] 8.279157

Note that you must use **na.rm = TRUE** to calculate the standard deviation if there are missing values in the dataset:

#create dataset with missing values data #attempt to find standard deviation sd(data) [1] NA #find standard deviation and specify to ignore missing values sd(data, na.rm = TRUE) [1] 9.179753

**Example 2: Calculate Standard Deviation of Column in Data Frame**

The following code shows how to calculate the standard deviation of a single column in a data frame:

#create data frame data frame(a=c(1, 3, 4, 6, 8, 9), b=c(7, 8, 8, 7, 13, 16), c=c(11, 13, 13, 18, 19, 22), d=c(12, 16, 18, 22, 29, 38)) #find standard deviation of column a sd(data$a) [1] 3.060501

**Example 3: Calculate Standard Deviation of Several Columns in Data Frame**

The following code shows how to calculate the standard deviation of several columns in a data frame:

#create data frame data frame(a=c(1, 3, 4, 6, 8, 9), b=c(7, 8, 8, 7, 13, 16), c=c(11, 13, 13, 18, 19, 22), d=c(12, 16, 18, 22, 29, 38)) #find standard deviation of specific columns in data frame apply(data[ , c('a', 'c', 'd')], 2, sd) a c d 3.060501 4.289522 9.544632

**Additional Resources**

How to Find the Range in R

How to Calculate Sample & Population Variance in R

How to Remove Outliers in R