# How to 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

  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 transform() 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.