#install.packages("dplyr")
PART A:
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
#1.Filter the mtcars dataset to include only cars with more than 6 cylinders using dplyr
data(mtcars)
mtcars_filtered=mtcars %>% filter(cyl > 6)
print(mtcars_filtered)
## 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
## Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4
## 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
## Cadillac Fleetwood 10.4 8 472.0 205 2.93 5.250 17.98 0 0 3 4
## Lincoln Continental 10.4 8 460.0 215 3.00 5.424 17.82 0 0 3 4
## Chrysler Imperial 14.7 8 440.0 230 3.23 5.345 17.42 0 0 3 4
## 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
## Camaro Z28 13.3 8 350.0 245 3.73 3.840 15.41 0 0 3 4
## 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
## Maserati Bora 15.0 8 301.0 335 3.54 3.570 14.60 0 1 5 8
#2.Select only the mpg, cyl, and hp columns from the mtcars dataset
mtcars_selected=mtcars %>% select(mpg, cyl, hp)
print(mtcars_selected)
## mpg cyl hp
## Mazda RX4 21.0 6 110
## Mazda RX4 Wag 21.0 6 110
## Datsun 710 22.8 4 93
## Hornet 4 Drive 21.4 6 110
## Hornet Sportabout 18.7 8 175
## Valiant 18.1 6 105
## Duster 360 14.3 8 245
## Merc 240D 24.4 4 62
## Merc 230 22.8 4 95
## Merc 280 19.2 6 123
## Merc 280C 17.8 6 123
## Merc 450SE 16.4 8 180
## Merc 450SL 17.3 8 180
## Merc 450SLC 15.2 8 180
## Cadillac Fleetwood 10.4 8 205
## Lincoln Continental 10.4 8 215
## Chrysler Imperial 14.7 8 230
## Fiat 128 32.4 4 66
## Honda Civic 30.4 4 52
## Toyota Corolla 33.9 4 65
## Toyota Corona 21.5 4 97
## Dodge Challenger 15.5 8 150
## AMC Javelin 15.2 8 150
## Camaro Z28 13.3 8 245
## Pontiac Firebird 19.2 8 175
## Fiat X1-9 27.3 4 66
## Porsche 914-2 26.0 4 91
## Lotus Europa 30.4 4 113
## Ford Pantera L 15.8 8 264
## Ferrari Dino 19.7 6 175
## Maserati Bora 15.0 8 335
## Volvo 142E 21.4 4 109
#3.How can you arrange the mtcars dataset in descending order of mpg using dplyr?
mtcars_arranged=mtcars %>% arrange(desc(mpg))
print(mtcars_arranged)
## 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
## Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2
## Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
## Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2
## Toyota Corona 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1
## Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
## Volvo 142E 21.4 4 121.0 109 4.11 2.780 18.60 1 1 4 2
## Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
## Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
## Ferrari Dino 19.7 6 145.0 175 3.62 2.770 15.50 0 1 5 6
## Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4
## Pontiac Firebird 19.2 8 400.0 175 3.08 3.845 17.05 0 0 3 2
## Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2
## Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1
## Merc 280C 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4 4
## Merc 450SL 17.3 8 275.8 180 3.07 3.730 17.60 0 0 3 3
## Merc 450SE 16.4 8 275.8 180 3.07 4.070 17.40 0 0 3 3
## Ford Pantera L 15.8 8 351.0 264 4.22 3.170 14.50 0 1 5 4
## Dodge Challenger 15.5 8 318.0 150 2.76 3.520 16.87 0 0 3 2
## Merc 450SLC 15.2 8 275.8 180 3.07 3.780 18.00 0 0 3 3
## AMC Javelin 15.2 8 304.0 150 3.15 3.435 17.30 0 0 3 2
## Maserati Bora 15.0 8 301.0 335 3.54 3.570 14.60 0 1 5 8
## Chrysler Imperial 14.7 8 440.0 230 3.23 5.345 17.42 0 0 3 4
## Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4
## Camaro Z28 13.3 8 350.0 245 3.73 3.840 15.41 0 0 3 4
## Cadillac Fleetwood 10.4 8 472.0 205 2.93 5.250 17.98 0 0 3 4
## Lincoln Continental 10.4 8 460.0 215 3.00 5.424 17.82 0 0 3 4
#4.Add a new column to the mtcars dataset that shows the hp per cyl (horsepower per cylinder)
mtcars_hp_per_cyl=mtcars %>% mutate(hp_per_cyl = hp / cyl)
print(mtcars_hp_per_cyl)
## mpg cyl disp hp drat wt qsec vs am gear carb
## Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
## Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
## Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
## Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
## Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2
## Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1
## Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4
## Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2
## Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2
## Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4
## Merc 280C 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4 4
## 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
## Cadillac Fleetwood 10.4 8 472.0 205 2.93 5.250 17.98 0 0 3 4
## Lincoln Continental 10.4 8 460.0 215 3.00 5.424 17.82 0 0 3 4
## Chrysler Imperial 14.7 8 440.0 230 3.23 5.345 17.42 0 0 3 4
## 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
## Toyota Corolla 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1
## Toyota Corona 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1
## 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
## Camaro Z28 13.3 8 350.0 245 3.73 3.840 15.41 0 0 3 4
## Pontiac Firebird 19.2 8 400.0 175 3.08 3.845 17.05 0 0 3 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
## Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2
## Ford Pantera L 15.8 8 351.0 264 4.22 3.170 14.50 0 1 5 4
## Ferrari Dino 19.7 6 145.0 175 3.62 2.770 15.50 0 1 5 6
## Maserati Bora 15.0 8 301.0 335 3.54 3.570 14.60 0 1 5 8
## Volvo 142E 21.4 4 121.0 109 4.11 2.780 18.60 1 1 4 2
## hp_per_cyl
## Mazda RX4 18.33333
## Mazda RX4 Wag 18.33333
## Datsun 710 23.25000
## Hornet 4 Drive 18.33333
## Hornet Sportabout 21.87500
## Valiant 17.50000
## Duster 360 30.62500
## Merc 240D 15.50000
## Merc 230 23.75000
## Merc 280 20.50000
## Merc 280C 20.50000
## Merc 450SE 22.50000
## Merc 450SL 22.50000
## Merc 450SLC 22.50000
## Cadillac Fleetwood 25.62500
## Lincoln Continental 26.87500
## Chrysler Imperial 28.75000
## Fiat 128 16.50000
## Honda Civic 13.00000
## Toyota Corolla 16.25000
## Toyota Corona 24.25000
## Dodge Challenger 18.75000
## AMC Javelin 18.75000
## Camaro Z28 30.62500
## Pontiac Firebird 21.87500
## Fiat X1-9 16.50000
## Porsche 914-2 22.75000
## Lotus Europa 28.25000
## Ford Pantera L 33.00000
## Ferrari Dino 29.16667
## Maserati Bora 41.87500
## Volvo 142E 27.25000
#5.Group the mtcars dataset by the number of cylinders (cyl) and calculate the average miles per gallon (mpg) for each group using dplyr
mtcars_grouped=mtcars %>% group_by(cyl) %>% summarise(avg_mpg = mean(mpg))
print(mtcars_grouped)
## # A tibble: 3 × 2
## cyl avg_mpg
## <dbl> <dbl>
## 1 4 26.7
## 2 6 19.7
## 3 8 15.1
#6.Find the car with the maximum horsepower (hp) in the mtcars dataset using dplyr
max_hp_car=mtcars %>% filter(hp == max(hp))
print(max_hp_car)
## mpg cyl disp hp drat wt qsec vs am gear carb
## Maserati Bora 15 8 301 335 3.54 3.57 14.6 0 1 5 8
#7.Calculate the total weight (wt) of cars with 8 cylinders in the mtcars dataset using dplyr
total_wt_8_cyl=mtcars %>% filter(cyl == 8) %>% summarise(total_wt = sum(wt))
print(total_wt_8_cyl)
## total_wt
## 1 55.989
#8.Find the number of cars that have a gear value of 4 using dplyr
num_cars_gear_4 = mtcars %>% filter(gear == 4) %>% summarise(count = n())
print(num_cars_gear_4)
## count
## 1 12
#9.Create a new dataframe that contains only cars with an mpg greater than 20 and with am equal to 1 (manual transmission) using dplyr
new_df=mtcars %>% filter(mpg > 20, am == 1)
print(new_df)
## mpg cyl disp hp drat wt qsec vs am gear carb
## Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
## Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
## Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 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
## Toyota Corolla 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1
## 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
## Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2
## Volvo 142E 21.4 4 121.0 109 4.11 2.780 18.60 1 1 4 2
#10.Calculate the average horsepower (hp) and the average weight (wt) for cars grouped by the number of gears (gear) using dplyr
avg_hp_wt_by_gear= mtcars %>% group_by(gear) %>% summarise(avg_hp = mean(hp), avg_wt = mean(wt))
print(avg_hp_wt_by_gear)
## # A tibble: 3 × 3
## gear avg_hp avg_wt
## <dbl> <dbl> <dbl>
## 1 3 176. 3.89
## 2 4 89.5 2.62
## 3 5 196. 2.63
PART B: #Create a dataframe as shown below
flights_df <- data.frame(
flight_id = c("AA101", "BA202", "DL303", "UA404", "SW505"),
origin = c("New York", "London", "Los Angeles", "Chicago", "Houston"),
destination = c("Los Angeles", "New York", "Chicago", "San Francisco", "Miami"),
duration_hours = c(6.0, 8.0, 4.5, 4.0, 3.0),
airline = c("American Airlines", "British Airways", "Delta", "United Airlines", "Southwest"),
flight_category = c("Long-Haul", "Long-Haul", "Short-Haul", "Short-Haul", "Short-Haul")
)
print(flights_df)
## flight_id origin destination duration_hours airline
## 1 AA101 New York Los Angeles 6.0 American Airlines
## 2 BA202 London New York 8.0 British Airways
## 3 DL303 Los Angeles Chicago 4.5 Delta
## 4 UA404 Chicago San Francisco 4.0 United Airlines
## 5 SW505 Houston Miami 3.0 Southwest
## flight_category
## 1 Long-Haul
## 2 Long-Haul
## 3 Short-Haul
## 4 Short-Haul
## 5 Short-Haul
#1.Which flights are operated by American Airlines?
american_flights=flights_df %>% filter(airline == "American Airlines")
print(american_flights)
## flight_id origin destination duration_hours airline
## 1 AA101 New York Los Angeles 6 American Airlines
## flight_category
## 1 Long-Haul
#2.What is the shortest flight duration in the DataFrame?
shortest_flight_duration=flights_df %>% summarise(min_duration = min(duration_hours))
print(shortest_flight_duration)
## min_duration
## 1 3
#3. What is the destination of the flight originating from London?
destination_from_london=flights_df %>% filter(origin == "London") %>% select(destination)
print(destination_from_london)
## destination
## 1 New York
#4.How many flights have a duration longer than 5 hours?
num_flights_longer_than_5=flights_df %>% filter(duration_hours > 5) %>% summarise(count = n())
print(num_flights_longer_than_5)
## count
## 1 2
#5.Which flight has the flight ID “UA404” and what is its destination?
flight_ua404=flights_df %>% filter(flight_id == "UA404") %>% select(destination)
print(flight_ua404 )
## destination
## 1 San Francisco
#6.What is the average duration of all flights in the DataFrame?
avg_flight_duration=flights_df %>% summarise(avg_duration = mean(duration_hours))
print(avg_flight_duration)
## avg_duration
## 1 5.1
#7.Which flight has the longest duration, and what is the airline?
longest_flight=flights_df %>% filter(duration_hours == max(duration_hours)) %>% select(flight_id, airline)
print(longest_flight)
## flight_id airline
## 1 BA202 British Airways
#8.What is the origin of the shortest flight?
origin_shortest_flight=flights_df %>% filter(duration_hours == min(duration_hours)) %>% select(origin)
print(origin_shortest_flight)
## origin
## 1 Houston
#9.Which flights are classified as “Short-Haul”?
short_haul_flights=flights_df %>% filter(flight_category == "Short-Haul")
print(short_haul_flights)
## flight_id origin destination duration_hours airline
## 1 DL303 Los Angeles Chicago 4.5 Delta
## 2 UA404 Chicago San Francisco 4.0 United Airlines
## 3 SW505 Houston Miami 3.0 Southwest
## flight_category
## 1 Short-Haul
## 2 Short-Haul
## 3 Short-Haul
#10. What are the flight IDs of all flights going to Chicago?
flights_to_chicago=flights_df %>% filter(destination == "Chicago") %>% select(flight_id)
print(flights_to_chicago)
## flight_id
## 1 DL303