Which of the following is valid for subsetting column cyl
from the mtcars
dataset?
$
(mtcars, cyl)All of the above options are correct. R provides three operators for subsetting [[
, [
, $
. mtcars$cyl
is equivalent to mtcars[['cyl']]
, but not to mtcars['cyl']
, as the latter one produces output of different class compared to former one. mtcars['cyl']
will give dataframe as a output whereas mtcars$cyl
will give numeric vector as an output.
These operators can be called as function also.
`$`(mtcars, cyl)
## [1] 6 6 4 6 8 6 8 4 4 6 6 8 8 8 8 8 8 4 4 4 4 8 8 8 8 4 4 4 8 6 8 4
`[[`(mtcars, 'cyl')
## [1] 6 6 4 6 8 6 8 4 4 6 6 8 8 8 8 8 8 4 4 4 4 8 8 8 8 4 4 4 8 6 8 4
`[`(mtcars, 'cyl')
## cyl
## Mazda RX4 6
## Mazda RX4 Wag 6
## Datsun 710 4
## Hornet 4 Drive 6
## Hornet Sportabout 8
## Valiant 6
## Duster 360 8
## Merc 240D 4
## Merc 230 4
## Merc 280 6
## Merc 280C 6
## Merc 450SE 8
## Merc 450SL 8
## Merc 450SLC 8
## Cadillac Fleetwood 8
## Lincoln Continental 8
## Chrysler Imperial 8
## Fiat 128 4
## Honda Civic 4
## Toyota Corolla 4
## Toyota Corona 4
## Dodge Challenger 8
## AMC Javelin 8
## Camaro Z28 8
## Pontiac Firebird 8
## Fiat X1-9 4
## Porsche 914-2 4
## Lotus Europa 4
## Ford Pantera L 8
## Ferrari Dino 6
## Maserati Bora 8
## Volvo 142E 4
Thanks for reading. If you like the question, how about some love and coffee: Buy me a coffee