2

Just self-learning R at the moment and have gotten a little stuck. I have a dataset and I want to summarize (find mean, max, etc) but only selecting those cases that have a particular value on a certain variable.

Alternatively, I guess the same outcome could be done by summarizing only certain rows in the dataset (ie summarize only rows 1 thru 20).

Could someone lend a helping hand? Thanks so much

killahtron
  • 595
  • 1
  • 4
  • 7
  • 1
    Welcome on SO! Please read [How to make a great R reproducible example?](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) and have a look at `?subset`. – sgibb Aug 31 '13 at 20:48
  • type this at the console: `summary(mtcars[1:20,])` – Ferdinand.kraft Aug 31 '13 at 20:52

1 Answers1

4
mydata<-mtcars

a. Find summary for rows 1 to 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  

b. Find summary when value of 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  
Metrics
  • 15,172
  • 7
  • 54
  • 83
  • Awesome, thanks ever so much! Do you have any recommendations on reading with regards to the symbols used in R? I'm familiar with statistics, just not the R language and I don't understand some of the symbols a lot of the time. – killahtron Aug 31 '13 at 21:05
  • Please go through the R [introductory manual](http://cran.r-project.org/doc/manuals/R-intro.html). `summary` gives the summary of the data. – Metrics Aug 31 '13 at 21:09