Q.1: Keep only cars with more than 6 cylinders and mpg greater than
15
& and , can be interchanged for and function
high_powered_cars <- mtcars %>%
filter(cyl >6 &
mpg >15)
print(high_powered_cars)
## mpg cyl disp hp drat wt qsec vs am gear carb
## Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2
## Merc 450SE 16.4 8 275.8 180 3.07 4.070 17.40 0 0 3 3
## Merc 450SL 17.3 8 275.8 180 3.07 3.730 17.60 0 0 3 3
## Merc 450SLC 15.2 8 275.8 180 3.07 3.780 18.00 0 0 3 3
## Dodge Challenger 15.5 8 318.0 150 2.76 3.520 16.87 0 0 3 2
## AMC Javelin 15.2 8 304.0 150 3.15 3.435 17.30 0 0 3 2
## Pontiac Firebird 19.2 8 400.0 175 3.08 3.845 17.05 0 0 3 2
## Ford Pantera L 15.8 8 351.0 264 4.22 3.170 14.50 0 1 5 4
Q.2: Select only the mpg, hp, and wt columns
efficiency_power <- mtcars %>%
select(mpg, hp, wt)
head(efficiency_power)
## mpg hp wt
## Mazda RX4 21.0 110 2.620
## Mazda RX4 Wag 21.0 110 2.875
## Datsun 710 22.8 93 2.320
## Hornet 4 Drive 21.4 110 3.215
## Hornet Sportabout 18.7 175 3.440
## Valiant 18.1 105 3.460
Q.3: Rename ‘mpg’ to ‘miles_per_gallon’ and ‘hp’ to
‘horsepower’
renamed_cars <- mtcars %>%
rename(miles_per_gallon = mpg,
horsepower = hp)
head(renamed_cars)
## miles_per_gallon cyl disp horsepower drat wt qsec vs am
## Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1
## Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1
## Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1
## Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0
## Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0
## Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0
## gear carb
## Mazda RX4 4 4
## Mazda RX4 Wag 4 4
## Datsun 710 4 1
## Hornet 4 Drive 3 1
## Hornet Sportabout 3 2
## Valiant 3 1
Q.4: Create a new column ‘efficiency_ratio’ (mpg / hp)
cars_with_ratio <- mtcars %>%
mutate(efficiency_ratio = mpg/hp, hp_weight_ratio = hp/wt)
head(cars_with_ratio)
## mpg cyl disp hp drat wt qsec vs am gear carb
## Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
## Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
## Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
## Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
## Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
## Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
## efficiency_ratio hp_weight_ratio
## Mazda RX4 0.1909091 41.98473
## Mazda RX4 Wag 0.1909091 38.26087
## Datsun 710 0.2451613 40.08621
## Hornet 4 Drive 0.1945455 34.21462
## Hornet Sportabout 0.1068571 50.87209
## Valiant 0.1723810 30.34682
Q.5: Sort cars by mpg in descending order, then by horsepower in
ascending order
sorted_cars <- mtcars %>%
arrange(desc(mpg), hp)
head(sorted_cars)
## mpg cyl disp hp drat wt qsec vs am gear carb
## Toyota Corolla 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1
## Fiat 128 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1
## Honda Civic 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2
## Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2
## Fiat X1-9 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4 1
## Porsche 914-2 26.0 4 120.3 91 4.43 2.140 16.70 0 1 5 2
Conclusion: Combine everything
combining_cars <- mtcars %>%
filter(cyl >6 &
mpg >15) %>%
select(mpg, hp, wt, am) %>%
rename(miles_per_gallon = mpg,
horsepower = hp) %>%
mutate(efficiency_ratio = miles_per_gallon/horsepower, hp_weight_ratio = horsepower/wt) %>%
arrange(desc(miles_per_gallon), horsepower) %>%
select(-am) # deletes a row
head(combining_cars)
## miles_per_gallon horsepower wt efficiency_ratio
## Pontiac Firebird 19.2 175 3.845 0.10971429
## Hornet Sportabout 18.7 175 3.440 0.10685714
## Merc 450SL 17.3 180 3.730 0.09611111
## Merc 450SE 16.4 180 4.070 0.09111111
## Ford Pantera L 15.8 264 3.170 0.05984848
## Dodge Challenger 15.5 150 3.520 0.10333333
## hp_weight_ratio
## Pontiac Firebird 45.51365
## Hornet Sportabout 50.87209
## Merc 450SL 48.25737
## Merc 450SE 44.22604
## Ford Pantera L 83.28076
## Dodge Challenger 42.61364