我正在通过哈德利的r4ds工作。我想知道为什么我发现的解决方案需要在代码中变异。是否可以使用汇总?我以为他们可以互换?
问题4:查看每天取消的飞行数量。有模式吗?取消飞行的比例是否与平均延误相关?
cancelled_delayed <-
flights %>%
mutate(cancelled = (is.na(arr_delay) | is.na(dep_delay))) %>%
group_by(year, month, day) %>%
summarise(prop_cancelled = mean(cancelled),
avg_dep_delay = mean(dep_delay, na.rm = TRUE))
ggplot(cancelled_delayed, aes(x = avg_dep_delay, prop_cancelled)) +
geom_point() +
geom_smooth()
答案 0 :(得分:3)
你可以跳过变异并在总结中做同样的事情:
library(dplyr)
cancelled_delayed <-
flights %>%
group_by(year, month, day) %>%
summarise(prop_cancelled = mean(is.na(arr_delay) | is.na(dep_delay)),
avg_dep_delay = mean(dep_delay, na.rm = TRUE))
但变异和总结根本不可互换。
mutate
用于在现有表中添加oder update一个或多个列,同时保持一般结构(行数)不变。另一方面,summarise
将每行的行数减少到1行,即在您的示例中每行每月组合行数。