How to find standard deviations for all columns of an R data frame?


To find the means of all columns in an R data frame, we can simply use colMeans function and it returns the mean. But for standard deviations, we do not have any direct function that can be used; therefore, we can use sd with apply and reference the columns to find the standard deviations for all column of an R data frame. For example, if we have a data frame df then the syntax using apply function to find the standard deviations for all columns will be apply(df,2,sd), here 2 refers to the columns. If we want to find the standard deviations for rows then we just need to replace this 2 with 1.

Example

Consider the below data frame −

 Live Demo

> set.seed(101)
> x1<-rnorm(20,0.5)
> x2<-rnorm(20,1.5)
> x3<-rnorm(20,2.5)
> df1<-data.frame(x1,x2,x3)
> df1

Output

      x1          x2       x3
1 0.1739635 1.33624433 2.9824588
2 1.0524619 2.20852210 3.2582138
3 -0.1749438 1.23201945 0.1806726
4 0.7143595 0.03607824 2.0404952
5 0.8107692 2.24443582 1.3946163
6 1.6739663 0.08960982 2.9029283
7 1.1187899 1.96706761 3.0689349
8 0.3872657 1.38067989 1.7939167
9 1.4170283 1.96723896 2.2099094
10 0.2767406 1.99813556 1.0161219
11 1.0264481 2.39493720 1.3497447
12 -0.2948444 1.77915200 2.2255288
13 1.9277555 2.50786575 3.0779010
14 -0.9668197 -0.57310649 1.1030974
15 0.2633166 2.68985338 3.2490577
16 0.3066620 0.77562578 1.4488133
17 -0.3497547 1.66798377 2.6653809
18 0.5584655 2.42033516 3.6298091
19 -0.3176704 -0.17160481 3.6737225
20 -1.5503078 1.94846907 2.0721368

Finding the standard deviations of all columns of df1 −

> apply(df1,2,sd)
x1 x2 x3
0.8667844 0.9730288 0.9738892

Let’s have a look at two more examples −

Example

 Live Demo

> y1<-rpois(20,2)
> y2<-rpois(20,5)
> y3<-rpois(20,8)
> df2<-data.frame(y1,y2,y3)
> df2

Output

 y1 y2 y3
1 1 9 14
2 1 4 9
3 0 9 11
4 2 4 8
5 1 8 6
6 3 3 18
7 2 5 6
8 2 6 12
9 2 1 3
10 0 4 9
11 2 3 9
12 1 5 15
13 1 6 8
14 1 9 10
15 2 2 12
16 2 3 15
17 2 5 10
18 4 7 11
19 2 5 13
20 3 8 9
> apply(df2,2,sd)
y1 y2 y3
0.978721 2.408319 3.545197

Example

 Live Demo

> z1<-runif(20,1,2) > z2<-runif(20,1,5) > z3<-runif(20,2,5) > df3<-data.frame(z1,z2,z3) > df3

Output

      z1       z2       z3
1 1.907492 3.422703 2.855133
2 1.762290 3.250390 3.475309
3 1.486333 2.107422 3.444077
4 1.250209 1.904570 3.314925
5 1.359045 4.934230 3.312890
6 1.008594 1.393549 2.558971
7 1.235712 4.518207 4.836347
8 1.106235 1.933838 2.436035
9 1.611034 4.089584 4.336852
10 1.204697 2.887437 4.440150
11 1.214610 2.635393 2.660501
12 1.016492 4.292893 2.949746
13 1.328194 3.139884 2.792373
14 1.269595 2.964845 3.565541
15 1.913872 1.057963 2.609570
16 1.417872 3.571295 3.959480
17 1.690566 2.281527 2.831667
18 1.900013 3.137568 3.226023
19 1.207709 4.816393 4.510174
20 1.461033 1.161574 3.305159
> apply(df3,2,sd)

Output

   z1       z2          z3
0.2907786 1.1771167 0.7123186

Updated on: 04-Sep-2020

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements