Skip to Content

Column Standard Deviation in R (Step by Step)

The R programming language comes with a number of helpful functions to work with the data stored in data structures like vectors, lists, dataframes, etc. In this tutorial, we will look at one such function that helps us get the standard deviation of the values in a column of an R dataframe.

How to get the standard deviation of an R dataframe column?

standard deviation of a dataframe column in R

You can use the built-in sd() function in R to compute the standard deviation of values in a dataframe column. Pass the column values as an argument to the function.

The following is the syntax –

sd(dataframe[[column_name]])

It returns the standard deviation of the passed vector.

Steps to compute the standard deviation of values in an R column

Let’s now look at a step-by-step example of using the above syntax to compute the std dev of a numeric column in R.

Step 1 – Create a dataframe

First, we will create an R dataframe that we will be using throughout this tutorial.

# create a dataframe
employees_df = data.frame(
  "Name"= c("Jim", "Dwight", "Angela", "Tobi", "Kevin"),
  "Age"= c(26, 28, 29, 32, 30)
)
# display the dataframe
print(employees_df)

Output:

  Team_A Team_B
1     70     65
2     80     95
3     90     91

We now have a dataframe containing information about some employees in an office. The dataframe has two columns – “Name” and “Age”.

Step 2 – Calculate the standard deviation column values using the sd() function

To calculate the standard deviation of values in a column, pass the column values as an argument to the sd() function. You can use the [[]] notation to access the values of a column.

Let’s compute the standard deviation in the “Age” column.

# std dev in "Age" column
sd_age = sd(employees_df[["Age"]])
# display the std dev
print(sd_age)

Output:

[1] 2.236068

We get the standard deviation in the “Age” column as 2.236068.

Standard deviation of a column with NA values in R

What if there are NA values in a column?

Let’s find out.

# create a dataframe
employees_df = data.frame(
  "Name"= c("Jim", "Dwight", "Angela", "Tobi", "Kevin"),
  "Age"= c(26, 28, NA, 32, 30)
)
# display the dataframe
print(employees_df)

Output:

    Name Age
1    Jim  26
2 Dwight  28
3 Angela  NA
4   Tobi  32
5  Kevin  30

Here, we created a new dataframe such that the “Age” column now contains some NA values.

Now, let’s apply the sd() function to the “Age” column.

# std dev in "Age" column
sd_age = sd(employees_df[["Age"]])
# display the std dev
print(sd_age)

Output:

[1] NA

We get NA as the standard deviation for the “Age” column. This happened because performing any mathematical operation with NA results in an NA in R.

If you want to calculate the standard deviation of a column with NA values, pass na.rm=TRUE to the sd() function to skip the NA values when computing the standard deviation.

# std dev in "Age" column
sd_age = sd(employees_df[["Age"]], na.rm=TRUE)
# display the std dev
print(sd_age)

Output:

[1] 2.581989

We now get the standard deviation of the “Age” column excluding the NA values.

Summary – Standard Deviation of Column Values in R

In this tutorial, we looked at how to compute the standard deviation of values in a column of an R dataframe. The following is a short summary of the steps –

  1. Create a dataframe (skip this step if you already have a dataframe on which you want to operate).
  2. Use the sd() function to compute the standard deviation of column values. Pass the column values vector as an argument.
  3. If your column contains any NA values, pass na.rm=TRUE to the sd() function to calculate the standard deviation excluding the NA values in the column.

You might also be interested in –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush is a data scientist passionate about using data to understand things better and make informed decisions. In the past, he's worked as a Data Scientist for ZS and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Tags

Tags