【发布时间】:2021-01-25 05:59:28
【问题描述】:
当我遇到这段代码时,我目前正在审查 R for Data Science。 这段代码的问题如下。我不明白这里 arrange 功能的必要性。 arrange 函数不只是重新排列行吗?
library(tidyverse)
library(nycflights13))
flights %>%
arrange(tailnum, year, month, day) %>%
group_by(tailnum) %>%
mutate(delay_gt1hr = dep_delay > 60) %>%
mutate(before_delay = cumsum(delay_gt1hr)) %>%
filter(before_delay < 1) %>%
count(sort = TRUE)
但是,无论有没有排列功能,它的输出都会有所不同,如下所示:
#with the arrange function
tailnum n
<chr> <int>
1 N954UW 206
2 N952UW 163
3 N957UW 142
4 N5FAAA 117
5 N38727 99
6 N3742C 98
7 N5EWAA 98
8 N705TW 97
9 N765US 97
10 N635JB 94
# ... with 3,745 more rows
和
#Without the arrange function
tailnum n
<chr> <int>
1 N952UW 215
2 N315NB 161
3 N705TW 160
4 N961UW 139
5 N713TW 128
6 N765US 122
7 N721TW 120
8 N5FAAA 117
9 N945UW 104
10 N19130 101
# ... with 3,774 more rows
如果您能帮助我理解这一点,我将不胜感激。为什么必须在此处包含 arrange 函数?
【问题讨论】: