10.07.2015 Views

Using R for Introductory Statistics : John Verzani

Using R for Introductory Statistics : John Verzani

Using R for Introductory Statistics : John Verzani

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Using</strong> R <strong>for</strong> introductory statistics 118returns a list with just the first component of 1st, whereas 1st [ [1]] returns the firstcomponent.For example, we define a list and then access the first element as a vector:> 1st = list(a=1:2,b=letters[1:2],c=FALSE)> lst[1]$a[1] 1 2A list with just the first component is returned as indicated by the $a that is printed in theoutput. This is the name of that component. When a list prints [[1]], it indicates that thefirst component has no name.The vector notation <strong>for</strong> data frames is different from that <strong>for</strong> lists, as the return valueremains a data frame. Suppose we do the above with a data frame using just the first twovariables.> df=data.frame(a=1:2,b=letters[1:2])> df [1]a1 12 2If df were treated as a list, and not a data frame, then the return value would be a list withthe specified top-level components. The different <strong>for</strong>matting of theTable 4.4 Different ways to access a data framemtcars mpg cyl disp hp drat wt qsec vsMazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0…Fiat 128 32.4 4 78.7 66 4.08 2.200 19.47 1Honda Civic 30.4 4 75.7 52 4.93 1.615 18.52 1Toyota Corolla 33.9 4 71.1 65 4.22 1.835 19.90 1…Maserati Bora 15.0 8 301.0 335 3.54 3.570 14.60 0Volvo 142E 21.4 4 121.0 109 4.11 2.780 18.60 1To access the row “Honda Civic”mtcars[’Honda Civic’,] By row namemtcars[’Honda’,]Can shorten the nameif unique matchmtcars[19,]It is also the 19throw in the data setTo access the column “mpg”mtcars[,’mpg’]By column namemtcars [,1] It is column 1mtcars$mpglist access by name

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!