如何使用 mean 函式而不是 colMeans 函式在 R 資料框中查詢多個列的平均值?


查詢 R 資料框中列平均值的最簡單方法是使用 colMeans 函式,但如果我們不想使用它,則可以使用 sapply 來實現。在使用 sapply 時,我們需要確保我們只選擇了資料框的數值列。檢視下面的示例以瞭解其工作原理。

示例 1

考慮基本 R 中的 CO2 資料框 -

線上演示

> head(CO2,20)

輸出

  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

查詢 CO2 的數值列的平均值 -

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

輸出

conc uptake
435.0000 27.2131

示例 2

考慮基本 R 中的 mtcars 資料框 -

線上演示

> head(mtcars,20)

輸出

          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

查詢 mtcars 的數值列的平均值 -

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

輸出

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

示例 3

考慮基本 R 中的 Orange 資料 -

線上演示

> head(Orange,20)

輸出

  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

查詢 Orange 的數值列的平均值 -

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

輸出

age circumference
922.1429 115.8571

示例 4

考慮基本 R 中的 ToothGrowth 資料 -

線上演示

> head(ToothGrowth,20)

輸出

  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

查詢 ToothGrowth 的數值列的平均值 -

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

輸出

len dose
18.813333 1.166667

示例 5

考慮基本 R 中的 USArrests 資料 -

線上演示

> head(USArrests,20)

輸出

           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

查詢 USArrests 的數值列的平均值 -

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

輸出

Murder Assault UrbanPop  Rape
7.788  170.760 65.540   21.232

更新於: 2021 年 3 月 4 日

268 次檢視

啟動您的 職業生涯

透過完成課程獲得認證

開始
廣告