在R中使用汇总函数并应用于特定情况

时间:2013-08-31 20:45:39

标签: r

此刻只是自学R并且有点陷入困境。我有一个数据集,我想总结(找到平均值,最大值等),但只选择那些对某个变量有特定值的情况。

或者,我想可以通过仅汇总数据集中的某些行来完成相同的结果(即仅汇总第1行到第20行)。

有人可以伸出援助之手吗?非常感谢

1 个答案:

答案 0 :(得分:4)

mydata<-mtcars

一个。查找第1行到第20行的摘要

 summary(mydata[1:20,])
      mpg             cyl           disp             hp             drat             wt             qsec             vs            am     
 Min.   :10.40   Min.   :4.0   Min.   : 71.1   Min.   : 52.0   Min.   :2.760   Min.   :1.615   Min.   :15.84   Min.   :0.0   Min.   :0.0  
 1st Qu.:16.10   1st Qu.:4.0   1st Qu.:145.2   1st Qu.: 94.5   1st Qu.:3.070   1st Qu.:2.811   1st Qu.:17.41   1st Qu.:0.0   1st Qu.:0.0  
 Median :18.95   Median :6.0   Median :196.3   Median :116.5   Median :3.460   Median :3.440   Median :18.15   Median :0.5   Median :0.0  
 Mean   :20.13   Mean   :6.2   Mean   :233.9   Mean   :136.2   Mean   :3.545   Mean   :3.398   Mean   :18.44   Mean   :0.5   Mean   :0.3  
 3rd Qu.:22.80   3rd Qu.:8.0   3rd Qu.:296.9   3rd Qu.:180.0   3rd Qu.:3.920   3rd Qu.:3.743   3rd Qu.:19.45   3rd Qu.:1.0   3rd Qu.:1.0  
 Max.   :33.90   Max.   :8.0   Max.   :472.0   Max.   :245.0   Max.   :4.930   Max.   :5.424   Max.   :22.90   Max.   :1.0   Max.   :1.0  
      gear          carb     
 Min.   :3.0   Min.   :1.00  
 1st Qu.:3.0   1st Qu.:1.75  
 Median :3.5   Median :3.00  
 Mean   :3.5   Mean   :2.70  
 3rd Qu.:4.0   3rd Qu.:4.00  
 Max.   :4.0   Max.   :4.00  

湾当cyl = 4

的值时查找摘要
summary(mydata[mydata$cyl==4,])
      mpg             cyl         disp              hp              drat             wt             qsec             vs               am        
 Min.   :21.40   Min.   :4   Min.   : 71.10   Min.   : 52.00   Min.   :3.690   Min.   :1.513   Min.   :16.70   Min.   :0.0000   Min.   :0.0000  
 1st Qu.:22.80   1st Qu.:4   1st Qu.: 78.85   1st Qu.: 65.50   1st Qu.:3.810   1st Qu.:1.885   1st Qu.:18.56   1st Qu.:1.0000   1st Qu.:0.5000  
 Median :26.00   Median :4   Median :108.00   Median : 91.00   Median :4.080   Median :2.200   Median :18.90   Median :1.0000   Median :1.0000  
 Mean   :26.66   Mean   :4   Mean   :105.14   Mean   : 82.64   Mean   :4.071   Mean   :2.286   Mean   :19.14   Mean   :0.9091   Mean   :0.7273  
 3rd Qu.:30.40   3rd Qu.:4   3rd Qu.:120.65   3rd Qu.: 96.00   3rd Qu.:4.165   3rd Qu.:2.623   3rd Qu.:19.95   3rd Qu.:1.0000   3rd Qu.:1.0000  
 Max.   :33.90   Max.   :4   Max.   :146.70   Max.   :113.00   Max.   :4.930   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