Q 1 : Load MtCars into workspace
car <- mtcars
Q 2 : Find number of rows and columns
nrow(car)
## [1] 32
ncol(car)
## [1] 11
Q 3 : Find out column and row names
colnames(car)
## [1] "mpg" "cyl" "disp" "hp" "drat" "wt" "qsec" "vs" "am" "gear"
## [11] "carb"
rownames(car)
## [1] "Mazda RX4" "Mazda RX4 Wag" "Datsun 710"
## [4] "Hornet 4 Drive" "Hornet Sportabout" "Valiant"
## [7] "Duster 360" "Merc 240D" "Merc 230"
## [10] "Merc 280" "Merc 280C" "Merc 450SE"
## [13] "Merc 450SL" "Merc 450SLC" "Cadillac Fleetwood"
## [16] "Lincoln Continental" "Chrysler Imperial" "Fiat 128"
## [19] "Honda Civic" "Toyota Corolla" "Toyota Corona"
## [22] "Dodge Challenger" "AMC Javelin" "Camaro Z28"
## [25] "Pontiac Firebird" "Fiat X1-9" "Porsche 914-2"
## [28] "Lotus Europa" "Ford Pantera L" "Ferrari Dino"
## [31] "Maserati Bora" "Volvo 142E"
Q 4 : What is type of data ?
typeof(car)
## [1] "list"
Q 5 : Synopsis of data ?
summary(car)
## mpg cyl disp hp
## Min. :10.40 Min. :4.000 Min. : 71.1 Min. : 52.0
## 1st Qu.:15.43 1st Qu.:4.000 1st Qu.:120.8 1st Qu.: 96.5
## Median :19.20 Median :6.000 Median :196.3 Median :123.0
## Mean :20.09 Mean :6.188 Mean :230.7 Mean :146.7
## 3rd Qu.:22.80 3rd Qu.:8.000 3rd Qu.:326.0 3rd Qu.:180.0
## Max. :33.90 Max. :8.000 Max. :472.0 Max. :335.0
## drat wt qsec vs
## Min. :2.760 Min. :1.513 Min. :14.50 Min. :0.0000
## 1st Qu.:3.080 1st Qu.:2.581 1st Qu.:16.89 1st Qu.:0.0000
## Median :3.695 Median :3.325 Median :17.71 Median :0.0000
## Mean :3.597 Mean :3.217 Mean :17.85 Mean :0.4375
## 3rd Qu.:3.920 3rd Qu.:3.610 3rd Qu.:18.90 3rd Qu.:1.0000
## Max. :4.930 Max. :5.424 Max. :22.90 Max. :1.0000
## am gear carb
## Min. :0.0000 Min. :3.000 Min. :1.000
## 1st Qu.:0.0000 1st Qu.:3.000 1st Qu.:2.000
## Median :0.0000 Median :4.000 Median :2.000
## Mean :0.4062 Mean :3.688 Mean :2.812
## 3rd Qu.:1.0000 3rd Qu.:4.000 3rd Qu.:4.000
## Max. :1.0000 Max. :5.000 Max. :8.000
Q 6 : Find out number of rows and columns of mtcars
nrow(mtcars)
## [1] 32
ncol(mtcars)
## [1] 11
Q 7 : Print the column values of qsec
car$qsec
## [1] 16.46 17.02 18.61 19.44 17.02 20.22 15.84 20.00 22.90 18.30 18.90
## [12] 17.40 17.60 18.00 17.98 17.82 17.42 19.47 18.52 19.90 20.01 16.87
## [23] 17.30 15.41 17.05 18.90 16.70 16.90 14.50 15.50 14.60 18.60
Q 8 : Print the row values of Datsun 710
car['Datsun 710',]
Q 9 : Print the values in Columns 2,7,11
car[,c(2,7,11)]
Q 10 : Print the values rows ‘Datsun 710’,‘Volvo 142E’, ‘Fiat 128’.
car[c('Datsun 710','Volvo 142E', 'Fiat 128'),]
Q 11 : What is the mileage of ‘Datasun 710’.
car['Datsun 710','mpg']
## [1] 22.8
Q 12 : What is mileage of all automatic cars.
car$mpg[car$am ==1]
## [1] 21.0 21.0 22.8 32.4 30.4 33.9 27.3 26.0 30.4 15.8 19.7 15.0 21.4
Q 13 : What are the weights of cars, which have 5 gears.
car$wt[car$gear == 5]
## [1] 2.140 1.513 3.170 2.770 3.570
Q 14 : Sort the dataframe in alphabetic order of rownames.
car[sort(row.names(car)),]
Q 15 : Sort the data according to decreasing order of gears
car[sort(car$gear),]
Q 16 : Sort the data according to automatic and mannual and within automatic and manual sort alphabetic.
car_sorted <- car[sort(row.names(car)),]
am1 <- car_sorted[car_sorted$am ==1,]
am0 <- car_sorted[car_sorted$am ==0,]
rbind(am1,am0)
Q 17 : Re-label the entries in columns vs, As ‘V-shaped’ for V, AND ‘Straight’ for S.
car$vs[car$vs == 0] = 'S Shaped'
cars_s <- car
cars_s$vs[cars_s$vs == 1] = 'V Shaped'
cars_v <- cars_s
Q 18 : Save the row names into a new variable. Find the data type of new variable.
rownames <- row.names(mtcars)
rownames
## [1] "Mazda RX4" "Mazda RX4 Wag" "Datsun 710"
## [4] "Hornet 4 Drive" "Hornet Sportabout" "Valiant"
## [7] "Duster 360" "Merc 240D" "Merc 230"
## [10] "Merc 280" "Merc 280C" "Merc 450SE"
## [13] "Merc 450SL" "Merc 450SLC" "Cadillac Fleetwood"
## [16] "Lincoln Continental" "Chrysler Imperial" "Fiat 128"
## [19] "Honda Civic" "Toyota Corolla" "Toyota Corona"
## [22] "Dodge Challenger" "AMC Javelin" "Camaro Z28"
## [25] "Pontiac Firebird" "Fiat X1-9" "Porsche 914-2"
## [28] "Lotus Europa" "Ford Pantera L" "Ferrari Dino"
## [31] "Maserati Bora" "Volvo 142E"
typeof(rownames)
## [1] "character"