library(MASS)
data("Cars93")
head(Cars93)Take-out some of the columns
Cars93[,c("Model", "Type", "Price")]Alternatively, pwede rin Integers specifying the particular columns
Cars93[,c(2,3,5)]Cars93[,which(colnames(Cars93)=="Model")] [1] Integra Legend 90 100 535i Century
[7] LeSabre Roadmaster Riviera DeVille Seville Cavalier
[13] Corsica Camaro Lumina Lumina_APV Astro Caprice
[19] Corvette Concorde LeBaron Imperial Colt Shadow
[25] Spirit Caravan Dynasty Stealth Summit Vision
[31] Festiva Escort Tempo Mustang Probe Aerostar
[37] Taurus Crown_Victoria Metro Storm Prelude Civic
[43] Accord Excel Elantra Scoupe Sonata Q45
[49] ES300 SC300 Continental Town_Car 323 Protege
[55] 626 MPV RX-7 190E 300E Capri
[61] Cougar Mirage Diamante Sentra Altima Quest
[67] Maxima Achieva Cutlass_Ciera Silhouette Eighty-Eight Laser
[73] LeMans Sunbird Firebird Grand_Prix Bonneville 900
[79] SL Justy Loyale Legacy Swift Tercel
[85] Celica Camry Previa Fox Eurovan Passat
[91] Corrado 240 850
93 Levels: 100 190E 240 300E 323 535i 626 850 90 900 Accord Achieva Aerostar Altima ... Vision
Find the index number of the column we’re looking for
which(colnames(Cars93)=="Model")[1] 2
Search for a specific cylinder type
Cars93[which(Cars93$Cylinders=="rotary"),]attach(Cars93)Subset rows and columns simultaneously
Cars93[which(Cylinders==8), c("Model","Price")]Now we can compare the means of those two different groups of cars We take out the “Model” column because we can’t get the mean of that column
mean(Cars93[which(Cars93$Cylinders==6), c("Price")])[1] 24.38065
mean(Cars93[which(Cars93$Cylinders==8), c("Price")])[1] 33.78571
We find above that cars with 8 cylinders are a more expensive on average
Create a subset
Ford <- Cars93[which(Cars93$Manufacturer=="Ford"),]FordForddata <- Cars93[which(Cars93$Manufacturer=="Ford"), c("Weight", "Origin", "Price")]
ForddataUse “-” or “!” to exclude data.
Ford[-which(Cars93$Cylinders==6),]