我有以下数据集
flight DateTime
<chr> <dttm>
1 1 2016-08-16 07:56:06
2 1 2016-08-16 07:57:10
3 1 2016-08-16 07:57:07
4 2 2016-08-15 18:35:09
5 2 2016-08-15 18:39:51
6 2 2016-08-15 18:46:53
7 3 2016-08-16 14:02:33
8 3 2016-08-16 13:25:10
9 3 2016-08-16 13:39:43
dt <- structure(list(flight = c("1", "1", "1", "2", "2", "2", "3",
"3", "3"), DateTime = structure(c(1471334352, 1471334210, 1471334262,
1471286072, 1471284963, 1471286347, 1471356056, 1471355584, 1471353810
), class = c("POSIXct", "POSIXt"), tzone = "UTC")), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -9L))
我正在尝试使用tidyverse计算每次飞行的持续时间(例如以小时为单位)。到目前为止,我已经尝试了下面的代码,但我怀疑有更简洁的方法来获得相同的结果,例如我可以summarise()
的函数?
dt %>% group_by(flight) %>%
filter(DateTime == max(DateTime) | DateTime == min(DateTime)) %>%
mutate(dur = if_else(DateTime == max(DateTime), 'Max', 'Min')) %>%
spread(dur, DateTime) %>% mutate(duration = Max - Min)
# A tibble: 3 x 4
# Groups: flight [3]
flight Max Min duration
<chr> <dttm> <dttm> <time>
1 1 2016-08-16 07:59:12 2016-08-16 07:56:50 2.36666666666667
2 2 2016-08-15 18:39:07 2016-08-15 18:16:03 23.0666666666667
3 3 2016-08-16 14:00:56 2016-08-16 13:23:30 37.4333333333333
答案 0 :(得分:3)
您不必使用点差,如果您的目标只是计算时差,则效果相同:
dt %>%
group_by(flight) %>%
summarise(duration = difftime(min(DateTime),max(DateTime),units = "hours"))