This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
hflights[c('ActualElapsedTime','ArrDelay','DepDelay')] | |
# Equivalently, using dplyr: | |
select(hflights, ActualElapsedTime, ArrDelay, DepDelay) | |
# Print out a tbl with the four columns of hflights related to delay | |
select(hflights, ActualElapsedTime, AirTime, ArrDelay, DepDelay) | |
# Print out hflights, nothing has changed! | |
hflights |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Helper functions used with dplyr | |
# Print out a tbl containing just ArrDelay and DepDelay | |
select(hflights, ArrDelay, DepDelay) | |
# Use a combination of helper functions and variable names to print out | |
# only the UniqueCarrier, FlightNum, TailNum, Cancelled, and CancellationCode | |
# columns of hflights | |
select(hflights, UniqueCarrier, FlightNum, contains("Tail"), contains("Cancel")) | |
# Find the most concise way to return the following columns with select and its |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Some comparisons to basic R | |
# both hflights and dplyr are available | |
ex1r <- hflights[c("TaxiIn","TaxiOut","Distance")] | |
ex1d <- select(hflights, TaxiIn, TaxiOut, Distance) | |
ex2r <- hflights[c("Year","Month","DayOfWeek","DepTime","ArrTime")] | |
ex2d <- select(hflights, Year:ArrTime, -DayofMonth) | |
ex3r <- hflights[c("TailNum","TaxiIn","TaxiOut")] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Add the new variable ActualGroundTime to a copy of hflights and save the result as g1. | |
g1 <- mutate(hflights, ActualGroundTime = ActualElapsedTime - AirTime) | |
# Add the new variable GroundTime to a g1. Save the result as g2. | |
g2 <- mutate(g1, GroundTime = TaxiIn + TaxiOut) | |
# Add the new variable AverageSpeed to g2. Save the result as g3. | |
g3 <- mutate(g2, AverageSpeed = Distance / AirTime * 60) | |
# Print out g3 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Add a second variable loss_percent to the dataset: m1 | |
m1 <- mutate(hflights, loss = ArrDelay - DepDelay, loss_percent = ((ArrDelay - DepDelay)/DepDelay)*100) | |
# mutate() allows you to use a new variable while creating a next variable in the same call | |
# Copy and adapt the previous command to reduce redendancy: m2 | |
m2 <- mutate(hflights, loss = ArrDelay - DepDelay, loss_percent = (loss/DepDelay) * 100 ) | |
# Add the three variables as described in the third instruction: m3 | |
m3 <- mutate(hflights, TotalTaxi = TaxiIn + TaxiOut, ActualGroundTime = ActualElapsedTime - AirTime, Diff = TotalTaxi - ActualGroundTime) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Print out all flights in hflights that traveled 3000 or more miles | |
filter(hflights, Distance > 3000) | |
# All flights flown by one of JetBlue, Southwest, or Delta | |
filter(hflights, UniqueCarrier %in% c('JetBlue', 'Southwest', 'Delta')) | |
# All flights where taxiing took longer than flying | |
filter(hflights, TaxiIn + TaxiOut > AirTime) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Combining tests using boolean operators | |
# All flights that departed before 5am or arrived after 10pm | |
filter(hflights, DepTime < 500 | ArrTime > 2200 ) | |
# All flights that departed late but arrived ahead of schedule | |
filter(hflights, DepDelay > 0 & ArrDelay < 0) | |
# All cancelled weekend flights | |
filter(hflights, DayOfWeek %in% c(6,7) & Cancelled == 1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Summarizing Exercise | |
# Select the flights that had JFK as their destination: c1 | |
c1 <- filter(hflights, Dest == 'JFK') | |
# Combine the Year, Month and DayofMonth variables to create a Date column: c2 | |
c2 <- mutate(c1, Date = paste(Year, Month, DayofMonth, sep = "-")) | |
# Print out a selection of columns of c2 | |
select(c2, Date, DepTime, ArrTime, TailNum) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Definition of dtc | |
dtc <- filter(hflights, Cancelled == 1, !is.na(DepDelay)) | |
# Arrange dtc by departure delays | |
arrange(dtc, DepDelay) | |
# Arrange dtc so that cancellation reasons are grouped | |
arrange(dtc, CancellationCode) | |
# Arrange dtc according to carrier and departure delays |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Print out a summary with variables min_dist and max_dist | |
summarize(hflights, min_dist = min(Distance), max_dist = max(Distance)) | |
# Print out a summary with variable max_div | |
summarize(filter(hflights, Diverted == 1), max_div = max(Distance)) | |
# Remove rows that have NA ArrDelay: temp1 | |
temp1 <- filter(hflights, !is.na(ArrDelay)) | |
# Generate summary about ArrDelay column of temp1 |