#install.packages("dplyr")
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
PART - A 1.Filter the mtcars dataset to include only cars with more than 6 cylinders using dplyr
df=data(mtcars)
df_filtered <-mtcars%>%filter(cyl>6)
print(df_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
df1<-mtcars%>%select(mpg,cyl,hp)
print(df1)
## 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
df_desc<-mtcars%>%arrange(desc(mpg))
print(df_desc)
## 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
df2=mtcars%>%mutate(hp_per_cyl=hp/cyl)
print(df2)
## 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
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
df_hpmax<-mtcars%>%filter(hp==max(hp))
print(df_hpmax)
## 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
df_wt<-mtcars%>%filter(cyl==8)%>%summarise(total_wt=sum(wt))
df_wt
## total_wt
## 1 55.989
num_cars<-mtcars%>%filter(gear==4)%>%summarise(count=n())
num_cars
## count
## 1 12
new_df <- mtcars %>% filter(mpg > 20, am == 1)
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
df_avg_hp<-mtcars%>%group_by(gear)%>%summarise(avg_hp=mean(hp),avg_wt=mean(wt))
df_avg_hp
## # 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
df_flight <-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"))
df_flight
## 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
american_flights<-df_flight%>%filter(airline=="American Airlines")
american_flights
## flight_id origin destination duration_hours airline
## 1 AA101 New york Los Angeles 6 American Airlines
## flight_category
## 1 Long-Haul
Question 2: What is the shortest flight duration in the DataFrame?
shortest_dur <-df_flight%>%summarise(min_duration=min(duration_hours))
shortest_dur
## min_duration
## 1 3
Question 3: What is the destination of the flight originating from London?
flight_lon <-df_flight%>%filter(origin=="London")%>%select(destination)
flight_lon
## destination
## 1 New york
Question 4: How many flights have a duration longer than 5 hours?
dur_5 <-df_flight%>%filter(duration_hours>5)%>%summarise(count=n())
dur_5
## count
## 1 2
Question 5: Which flight has the flight ID “UA404” and what is its destination?
df_id = df_flight%>%filter(flight_id=="UA404")%>%select(airline,destination)
df_id
## airline destination
## 1 United Airlines San Francisco
Question 6: What is the average duration of all flights in the DataFrame?
avg_dur <- df_flight%>%summarise(avg_dur=mean(duration_hours))
avg_dur
## avg_dur
## 1 5.1
Question 7: Which flight has the longest duration, and what is the airline?
longest_flight <- df_flight%>% filter(duration_hours == max(duration_hours)) %>% select(flight_id, airline)
longest_flight
## flight_id airline
## 1 BA202 British Airways
Question 8: What is the origin of the shortest flight?
origin_shortest_flight <- df_flight %>% filter(duration_hours == min(duration_hours)) %>% select(origin)
origin_shortest_flight
## origin
## 1 Houston
Question 9: Which flights are classified as “Short-Haul”?
short_haul_flights <- df_flight %>% filter(flight_category == "Short-Haul")
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
Question 10: What are the flight IDs of all flights going to Chicago?
flights_to_chicago <- df_flight %>% filter(destination == "Chicago") %>% select(flight_id)
flights_to_chicago
## flight_id
## 1 DL303