“How can the standard deviation of rows in R be calculated?”

How can the standard deviation of rows in R be calculated?”

The standard deviation of rows in R can be calculated using the built-in function “sd()” which takes a data frame as its input. This function computes the standard deviation of the numerical values in each row of the data frame and returns a vector containing the result for each row. Alternatively, the “apply()” function can also be used with the “sd” function as its argument to calculate the standard deviation for each row of a data frame. The standard deviation of rows provides a measure of the variability within each row and can be useful in analyzing and comparing the spread of data across different rows in a dataset.

Calculate Standard Deviation of Rows in R


You can use the following basic syntax to calculate the standard deviation of rows in R:

row_stdev <- apply(df, 1, sd, na.rm=TRUE)

The following example shows how to use this syntax in R.

Example: Calculate Standard Deviation of Rows in R

Suppose we have the following data frame in R:

#create data frame
df <- data.frame(game1=c(12, 15, 15, 18, 29, 30, 31),
                 game2=c(15, 17, 17, 16, 29, 8, 14),
                 game3=c(8, 22, 27, 35, 29, 22, 17))

#view data frame
df

  game1 game2 game3
1    12    15     8
2    15    17    22
3    15    17    27
4    18    16    35
5    29    29    29
6    30     8    22
7    31    14    17

We can use the following syntax to calculate the standard deviation of the values in each row:

#calculate standard deviation of each row
row_stdev <- apply(df, 1, sd, na.rm=TRUE)

#view standard deviation of each row
row_stdev

[1]  3.511885  3.605551  6.429101 10.440307  0.000000 11.135529  9.073772

From the output we can see:

  • The standard deviation of values in the first row is 3.511885.
  • The standard deviation of values in the second row is 3.605551.
  • The standard deviation of values in the third row is 6.429101.

And so on.

If we’d like, we can also use the function to add a new column to the data frame that shows the standard deviation of values in each row:

#add column that displays standard deviation of each row
df <- transform(df, row_stdev=apply(df, 1, sd, na.rm=TRUE))

#view updated data frame
df

  game1 game2 game3 row_stdev
1    12    15     8  3.511885
2    15    17    22  3.605551
3    15    17    27  6.429101
4    18    16    35 10.440307
5    29    29    29  0.000000
6    30     8    22 11.135529
7    31    14    17  9.073772

The new column called row_stdev displays the standard deviation of values in each row.

Note: The standard deviation of values in row 5 is equal to zero because each of the values is the same, thus there is no “deviation” at all in the values.

Related:

Cite this article

stats writer (2024). How can the standard deviation of rows in R be calculated?”. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-the-standard-deviation-of-rows-in-r-be-calculated/

stats writer. "How can the standard deviation of rows in R be calculated?”." PSYCHOLOGICAL SCALES, 26 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-the-standard-deviation-of-rows-in-r-be-calculated/.

stats writer. "How can the standard deviation of rows in R be calculated?”." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-the-standard-deviation-of-rows-in-r-be-calculated/.

stats writer (2024) 'How can the standard deviation of rows in R be calculated?”', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-the-standard-deviation-of-rows-in-r-be-calculated/.

[1] stats writer, "How can the standard deviation of rows in R be calculated?”," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can the standard deviation of rows in R be calculated?”. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top