How to find the mean for multiple columns in an R data frame using mean function not colMeans?


The easiest way to find the column means of an R data frame is to use colMeans function but if we do not want to use it then it can be done with the help of sapply. While using sapply, we need to make sure that we are selecting only numerical columns of the data frame. Check out the below examples to understand how it works.

Example1

Consider the CO2 data frame in base R −

Live Demo

> head(CO2,20)

Output

  Plant  Type  Treatment  conc  uptake
1   Qn1 Quebec nonchilled  95   16.0
2   Qn1 Quebec nonchilled  175  30.4
3   Qn1 Quebec nonchilled  250  34.8
4   Qn1 Quebec nonchilled  350  37.2
5   Qn1 Quebec nonchilled  500  35.3
6   Qn1 Quebec nonchilled  675  39.2
7   Qn1 Quebec nonchilled  1000 39.7
8   Qn2 Quebec nonchilled  95   13.6
9   Qn2 Quebec nonchilled  175  27.3
10  Qn2 Quebec nonchilled  250  37.1
11  Qn2 Quebec nonchilled  350  41.8
12  Qn2 Quebec nonchilled  500  40.6
13  Qn2 Quebec nonchilled  675  41.4
14  Qn2 Quebec nonchilled  1000 44.3
15  Qn3 Quebec nonchilled  95   16.2
16  Qn3 Quebec nonchilled  175  32.4
17  Qn3 Quebec nonchilled  250  40.3
18  Qn3 Quebec nonchilled  350  42.1
19  Qn3 Quebec nonchilled  500  42.9
20  Qn3 Quebec nonchilled  675  43.9

Finding mean of numerical columns of CO2 −

> sapply(Filter(is.numeric,CO2),mean)

Output

conc uptake
435.0000 27.2131

Example2

Consider the mtcars data frame in base R −

Live Demo

> head(mtcars,20)

Output

          mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2
Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1
Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4
Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2
Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2
Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4
Merc 280C 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4 4
Merc 450SE 16.4 8 275.8 180 3.07 4.070 17.40 0 0 3 3
Merc 450SL 17.3 8 275.8 180 3.07 3.730 17.60 0 0 3 3
Merc 450SLC 15.2 8 275.8 180 3.07 3.780 18.00 0 0 3 3
Cadillac Fleetwood 10.4 8 472.0 205 2.93 5.250 17.98 0 0 3 4
Lincoln Continental 10.4 8 460.0 215 3.00 5.424 17.82 0 0 3 4
Chrysler Imperial 14.7 8 440.0 230 3.23 5.345 17.42 0 0 3 4
Fiat 128 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1
Honda Civic 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2
Toyota Corolla 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1

Finding mean of numerical columns of mtcars −

> sapply(Filter(is.numeric,mtcars),mean)

Output

mpg cyl disp hp drat wt qsec
20.090625 6.187500 230.721875 146.687500 3.596563 3.217250 17.848750
vs am gear carb
0.437500 0.406250 3.687500 2.812500

Example3

Consider the Orange data in base R −

Live Demo

> head(Orange,20)

Output

  Tree age   circumference
1    1 118   30
2    1 484   58
3    1 664   87
4    1 1004  115
5    1 1231  120
6    1 1372  142
7    1 1582  145
8    2 118   33
9    2 484   69
10   2 664   111
11   2 1004  156
12   2 1231  172
13   2 1372  203
14   2 1582  203
15   3 118   30
16   3 484   51
17   3 664   75
18   3 1004  108
19   3 1231  115
20   3 1372  139

Finding mean of numerical columns of Orange −

> sapply(Filter(is.numeric,Orange),mean)

Output

age circumference
922.1429 115.8571

Example4

Consider ToothGrowth data in base R −

Live Demo

> head(ToothGrowth,20)

Output

  len  supp dose
1 4.2   VC  0.5
2 11.5  VC  0.5
3 7.3   VC  0.5
4 5.8   VC  0.5
5 6.4   VC  0.5
6 10.0  VC  0.5
7 11.2  VC  0.5
8 11.2  VC  0.5
9 5.2   VC  0.5
10 7.0  VC  0.5
11 16.5 VC  1.0
12 16.5 VC  1.0
13 15.2 VC  1.0
14 17.3 VC  1.0
15 22.5 VC  1.0
16 17.3 VC  1.0
17 13.6 VC  1.0
18 14.5 VC  1.0
19 18.8 VC  1.0
20 15.5 VC  1.0

Finding mean of numerical columns of ToothGrowth −

> sapply(Filter(is.numeric,ToothGrowth),mean)

Output

len dose
18.813333 1.166667

Example5

Consider USArrests data in base R −

Live Demo

> head(USArrests,20)

Output

           Murder  Assault UrbanPop  Rape
Alabama     13.2   236       58      21.2
Alaska      10.0   263       48      44.5
Arizona     8.1    294       80      31.0
Arkansas    8.8    190       50      19.5
California  9.0    276       91      40.6
Colorado    7.9    204       78      38.7
Connecticut 3.3    110       77      11.1
Delaware    5.9    238       72      15.8
Florida    15.4    335       80      31.9
Georgia    17.4    211       60      25.8
Hawaii      5.3    46        83      20.2
Idaho       2.6    120       54      14.2
Illinois   10.4    249       83      24.0
Indiana     7.2    113       65      21.0
Iowa        2.2    56        57      11.3
Kansas      6.0    115       66      18.0
Kentucky    9.7    109       52      16.3
Louisiana  15.4    249       66      22.2
Maine       2.1    83        51       7.8
Maryland   11.3    300       67      27.8

Finding mean of numerical columns of USArrests −

> sapply(Filter(is.numeric,USArrests),mean)

Output

Murder Assault UrbanPop  Rape
7.788  170.760 65.540   21.232

Updated on: 04-Mar-2021

228 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements