←
PDF 270 / 520 For month() and wday() you can set label = TRUE to return the abbreviated name of the month or d
→
English · PDF 270
Original PDF page 270
中文 · PDF 270

对于 month() 和 wday(),你可以设置 label = TRUE 来返回月份或星期几的缩写名称。设置 abbr = FALSE 则返回完整名称:

month(datetime, label = TRUE) #> [1] Jul #> 12 Levels: Jan < Feb < Mar < Apr < May < Jun < ... < Dec wday(datetime, label = TRUE, abbr = FALSE) #> [1] Friday #> 7 Levels: Sunday < Monday < Tuesday < ... < Saturday

我们可以使用 wday() 来查看工作日出发的航班比周末更多:

flights_dt %>% mutate(wday = wday(dep_time, label = TRUE)) %>% ggplot(aes(x = wday)) + geom_bar()

如果我们按小时内的分钟数查看平均出发延误时间,会发现一个有趣的模式。看起来在第 20–30 分钟和第 50–60 分钟起飞的航班,其延误时间远低于该小时内的其他时间!

flights_dt %>% mutate(minute = minute(dep_time)) %>% group_by(minute) %>% summarize( avg_delay = mean(arr_delay, na.rm = TRUE), n = n()) %>% ggplot(aes(minute, avg_delay)) + geom_line()