if (require("data.table") && require("hflights")) {
hflights2 <- tbl_dt(hflights)
by_dest <- group_by(hflights2, Dest)
filter(by_dest, ArrDelay == max(ArrDelay, na.rm = TRUE))
summarise(by_dest, arr = mean(ArrDelay, na.rm = TRUE))
# Normalise arrival and departure delays by airport
scaled <- mutate(by_dest, arr_z = scale(ArrDelay), dep_z = scale(DepDelay))
select(scaled, Year:DayOfWeek, Dest, arr_z:dep_z)
arrange(by_dest, desc(ArrDelay))
select(by_dest, -(DayOfWeek:TailNum))
# All manip functions preserve grouping structure, except for summarise
# which removes a grouping level
by_day <- group_by(hflights, Year, Month, DayofMonth)
by_month <- summarise(by_day, delayed = sum(ArrDelay > 0, na.rm = TRUE))
by_month
summarise(by_month, delayed = sum(delayed))
# You can also manually ungroup:
ungroup(by_day)
}
Run the code above in your browser using DataLab