How to Calculate Standard deviation in R:

To calculate the standard deviation in R, use the sd() function. The sd() is a built-in R function that accepts the input object and computes the standard deviation of the values provided in the object.  It takes numerical vectors and logical arguments and returns the standard deviation.

The standard deviation of a population is the square root of the population variance. It is the measure of the distribution of the values.

The symbol for the population standard deviation is Σ (sigma). Its formula is the following.

Calculate Standard Deviation in R using sd()

The square root of its variance calculates the standard deviation of an observation variable in R.

Syntax

sd(x, na.rm = FALSE)

Parameters

x: It is a numeric vector or an R object but not a factor coercible to numeric by as.double(x).

na.rm: It is logical. Should missing values be removed?

Example 1: Simple program of sd() function

Let’s find the sd of a vector. To create a numeric vector in R, use the c() function. Then, pass the vector to the sd() function as an argument to calculate the standard deviation of a vector.

# Create a numeric vector using c() function
v1 <- c(11, 21, 19, 46, 50)

# Calculate the standard deviation of the vector using sd() function
stddev <- sd(v1)

# Print the standard deviation using the print() function
print(stddev)

Output

[1] 17.4442

In the above example, we created a numeric vector v1 using the c() function containing values 11, 21, 19, 46, and 50.

We then use the sd() function to calculate the standard deviation of the v1 vector. The sd() function returns the standard deviation as a numeric value, which we store in the stddev variable.

Finally, we use the print() function to print the stddev variable, which displays the standard deviation of the v1 vector.

Example 2: Calculating the standard deviation of data set in R

We will find the standard deviation of the Petal.length of the iris dataset.

data(iris)
iris$Petal.Length
ln <- iris$Petal.Length
cat("The standard deviation of iris petal length is: ", "\n")
sd(ln)

Output

[1] 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 1.5 1.6 1.4 1.1 1.2 1.5 1.3 1.4
[19] 1.7 1.5 1.7 1.5 1.0 1.7 1.9 1.6 1.6 1.5 1.4 1.6 1.6 1.5 1.5 1.4 1.5 1.2
[37] 1.3 1.4 1.3 1.5 1.3 1.3 1.3 1.6 1.9 1.4 1.6 1.4 1.5 1.4 4.7 4.5 4.9 4.0
[55] 4.6 4.5 4.7 3.3 4.6 3.9 3.5 4.2 4.0 4.7 3.6 4.4 4.5 4.1 4.5 3.9 4.8 4.0
[73] 4.9 4.7 4.3 4.4 4.8 5.0 4.5 3.5 3.8 3.7 3.9 5.1 4.5 4.5 4.7 4.4 4.1 4.0
[91] 4.4 4.6 4.0 3.3 4.2 4.2 4.2 4.3 3.0 4.1 6.0 5.1 5.9 5.6 5.8 6.6 4.5 6.3
[109] 5.8 6.1 5.1 5.3 5.5 5.0 5.1 5.3 5.5 6.7 6.9 5.0 5.7 4.9 6.7 4.9 5.7 6.0
[127] 4.8 4.9 5.6 5.8 6.1 6.4 5.6 5.1 5.6 6.1 5.6 5.5 4.8 5.4 5.6 5.1 5.1 5.9
[145] 5.7 5.2 5.0 5.2 5.4 5.1

The standard deviation of iris petal length is:

[1] 1.765298

That is it. The standard deviation for the petal.length is 1.765298.

You can calculate the standard deviation without the sd() function.

sqrt(sum((ln - mean(ln)) ^ 2 / (length(ln) - 1)))

The complete code is the following.

data(iris)
iris$Petal.Length
ln <- iris$Petal.Length
cat("The standard deviation of iris petal length is: ", "\n")
sqrt(sum((ln - mean(ln)) ^ 2 / (length(ln) - 1)))

Output

[1] 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 1.5 1.6 1.4 1.1 1.2 1.5 1.3 1.4
[19] 1.7 1.5 1.7 1.5 1.0 1.7 1.9 1.6 1.6 1.5 1.4 1.6 1.6 1.5 1.5 1.4 1.5 1.2
[37] 1.3 1.4 1.3 1.5 1.3 1.3 1.3 1.6 1.9 1.4 1.6 1.4 1.5 1.4 4.7 4.5 4.9 4.0
[55] 4.6 4.5 4.7 3.3 4.6 3.9 3.5 4.2 4.0 4.7 3.6 4.4 4.5 4.1 4.5 3.9 4.8 4.0
[73] 4.9 4.7 4.3 4.4 4.8 5.0 4.5 3.5 3.8 3.7 3.9 5.1 4.5 4.5 4.7 4.4 4.1 4.0
[91] 4.4 4.6 4.0 3.3 4.2 4.2 4.2 4.3 3.0 4.1 6.0 5.1 5.9 5.6 5.8 6.6 4.5 6.3
[109] 5.8 6.1 5.1 5.3 5.5 5.0 5.1 5.3 5.5 6.7 6.9 5.0 5.7 4.9 6.7 4.9 5.7 6.0
[127] 4.8 4.9 5.6 5.8 6.1 6.4 5.6 5.1 5.6 6.1 5.6 5.5 4.8 5.4 5.6 5.1 5.1 5.9
[145] 5.7 5.2 5.0 5.2 5.4 5.1

The standard deviation of iris petal length is:

[1] 1.765298

Example 3: Calculating the Standard deviation of the Vector in R

To calculate the standard deviation of the vector, use the sd() function. To define a vector, use the c() function and pass the elements as arguments. You can also create a vector using the :(colon) operator.

vec <- 1:5
cat("The standard deviation of vector is", "\n")
sd(vec)

Output

The standard deviation of vector is
[1] 1.581139

And we get the standard deviation of the numeric vector, which in our example is 1.581139.

Example 4: Calculating the standard deviation of the Array in R

To calculate the standard deviation of an array in R, use the sd() function.

To create an array in R, use the array() function. The array() function takes a vector as an argument and uses the dim parameter to create an array.

rv <- c(19, 21)
rv2 <- c(46, 4)
arr <- array(c(rv, rv2), dim = c(2, 2, 2))
cat("The standard deviation of array is", "\n")
sd(arr)

Output

The standard deviation of array is
[1] 16.11565

Example 5: Calculating the Standard deviation of a data frame in R

To calculate the standard deviation of a data frame in R, use the sd() function. To create a data frame in R, use data.frame() function. We will find the standard deviation of a numerical column of the data frame.

df <- data.frame(service_id = c(1:5),
 service_name = c("Netflix", "Disney+", "HBOMAX", "Hulu", "Peacock"),
 service_price = c(18, 10, 15, 7, 12),
 stringsAsFactors = FALSE)
cat("The standard deviation of service_price is", "\n")
sd(df$service_price)

Output

[1] 4.27785

And we get the SD of the data frame column.

That’s it.

Leave a Comment