如何在R中按分类变量查找摘要?
要按分类变量查找摘要,我们可以按照以下步骤操作 -
使用内置数据集或创建新数据集。
使用 by 函数查找汇总统计信息。
使用内置数据集
让我们考虑以 R 为基础的 mtcars 数据集 -
data(mtcars)head(mtcars,25)
执行时,上述脚本生成以下内容output(this output will vary on your system due to randomization)-
mpg cyl disp hp drat wt qsec vs am gear carbMazda 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
Toyota Corona 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1
Dodge Challenger 15.5 8 318.0 150 2.76 3.520 16.87 0 0 3 2
AMC Javelin 15.2 8 304.0 150 3.15 3.435 17.30 0 0 3 2
Camaro Z28 13.3 8 350.0 245 3.73 3.840 15.41 0 0 3 4
Pontiac Firebird 19.2 8 400.0 175 3.08 3.845 17.05 0 0 3 2
查找基于分类列的汇总统计信息
使用 by 函数查找基于 mtcars 数据集的 cyl 列的汇总统计信息 -
data(mtcars)by(mtcars,factor(mtcars$cyl),summary)
输出
factor(mtcars$cyl): 4mpg cyl disp hp drat
Min. :21.40 Min. :4 Min. : 71.10 Min. : 52.00 Min. :3.690
1st Qu.:22.80 1st Qu.:4 1st Qu.: 78.85 1st Qu.: 65.50 1st Qu.:3.810
Median :26.00 Median :4 Median :108.00 Median : 91.00 Median :4.080
Mean :26.66 Mean :4 Mean :105.14 Mean : 82.64 Mean :4.071
3rd Qu.:30.40 3rd Qu.:4 3rd Qu.:120.65 3rd Qu.: 96.00 3rd Qu.:4.165
Max. :33.90 Max. :4 Max. :146.70 Max. :113.00 Max. :4.930
wt qsec vs am
Min. :1.513 Min. :16.70 Min. :0.0000 Min. :0.0000
1st Qu.:1.885 1st Qu.:18.56 1st Qu.:1.0000 1st Qu.:0.5000
Median :2.200 Median :18.90 Median :1.0000 Median :1.0000
Mean :2.286 Mean :19.14 Mean :0.9091 Mean :0.7273
3rd Qu.:2.623 3rd Qu.:19.95 3rd Qu.:1.0000 3rd Qu.:1.0000
Max. :3.190 Max. :22.90 Max. :1.0000 Max. :1.0000
gear carb
Min. :3.000 Min. :1.000
1st Qu.:4.000 1st Qu.:1.000
Median :4.000 Median :2.000
Mean :4.091 Mean :1.545
3rd Qu.:4.000 3rd Qu.:2.000
Max. :5.000 Max. :2.000
------------------------------------------------------------
factor(mtcars$cyl): 6
mpg cyl disp hp drat
Min. :17.80 Min. :6 Min. :145.0 Min. :105.0 Min. :2.760
1st Qu. :18.65 1st Qu.:6 1st Qu.:160.0 1st Qu.:110.0 1st Qu.:3.350
Median :19.70 Median :6 Median :167.6 Median :110.0 Median :3.900
Mean :19.74 Mean :6 Mean :183.3 Mean :122.3 Mean :3.586
3rd Qu.:21.00 3rd Qu.:6 3rd Qu.:196.3 3rd Qu.:123.0 3rd Qu.:3.910
Max. :21.40 Max. :6 Max. :258.0 Max. :175.0 Max. :3.920
wt qsec vs am
Min. :2.620 Min. :15.50 Min. :0.0000 Min. :0.0000
1st Qu.:2.822 1st Qu.:16.74 1st Qu.:0.0000 1st Qu.:0.0000
Median :3.215 Median :18.30 Median :1.0000 Median :0.0000
Mean :3.117 Mean :17.98 Mean :0.5714 Mean :0.4286
3rd Qu.:3.440 3rd Qu.:19.17 3rd Qu.:1.0000 3rd Qu.:1.0000
Max. :3.460 Max. :20.22 Max. :1.0000 Max. :1.0000
gear carb
Min. :3.000 Min. :1.000
1st Qu.:3.500 1st Qu.:2.500
Median :4.000 Median :4.000
Mean :3.857 Mean :3.429
3rd Qu.:4.000 3rd Qu.:4.000
Max. :5.000 Max. :6.000
------------------------------------------------------------
factor(mtcars$cyl): 8
mpg cyl disp hp drat
Min. :10.40 Min. :8 Min. :275.8 Min. :150.0 Min. :2.760
1st Qu.:14.40 1st Qu.:8 1st Qu.:301.8 1st Qu.:176.2 1st Qu.:3.070
Median :15.20 Median :8 Median :350.5 Median :192.5 Median :3.115
Mean :15.10 Mean :8 Mean :353.1 Mean :209.2 Mean :3.229
3rd Qu.:16.25 3rd Qu.:8 3rd Qu.:390.0 3rd Qu.:241.2 3rd Qu.:3.225
Max. :19.20 Max. :8 Max. :472.0 Max. :335.0 Max. :4.220
wt qsec vs am gear
Min. :3.170 Min. :14.50 Min. :0 Min. :0.0000 Min. :3.000
1st Qu.:3.533 1st Qu.:16.10 1st Qu.:0 1st Qu.:0.0000 1st Qu.:3.000
Median :3.755 Median :17.18 Median :0 Median :0.0000 Median :3.000
Mean :3.999 Mean :16.77 Mean :0 Mean :0.1429 Mean :3.286
3rd Qu.:4.014 3rd Qu.:17.55 3rd Qu.:0 3rd Qu.:0.0000 3rd Qu.:3.000
Max. :5.424 Max. :18.00 Max. :0 Max. :1.0000 Max. :5.000
carb
Min. :2.00
1st Qu.: 2.25
Median : 3.50
Mean : 3.50
3rd Qu.: 4.00
Max. : 8.00
以上是 如何在R中按分类变量查找摘要? 的全部内容, 来源链接: utcz.com/z/345804.html