我有一个如下所示的数据框:
POI LOCAL.DATETIME
1 1 2017-07-11 15:02:13
2 1 2017-07-11 15:20:28
3 2 2017-07-11 15:20:31
4 2 2017-07-11 15:21:13
5 3 2017-07-11 15:21:18
6 3 2017-07-11 15:21:21
7 2 2017-07-11 15:21:25
8 2 2017-07-11 15:21:59
9 1 2017-07-11 15:22:02
10 1 2017-07-11 15:22:05
我希望能够计算(可能是使用lubridate)在每个POI上花费的累积时间,并将它们组合成一个看起来像这样的表:
POI TOTAL.TIME
1 1 00:18:18
2 2 00:01:11
3 3 00:00:03
另外,我不知道如何处理POI之间的时间,比如第2行和第3行之间的3秒。我想我可能需要计算从第1行到第3行而不是第1行到第2行的时间
答案 0 :(得分:2)
要获得每个组周期的总时间,首先需要创建一个组索引。我使用rleid
中的data.table
您可以计算每个组中花费的总时间,然后使用sum
按初始POI进行汇总。
df <- read.table(text=" POI LOCAL.DATETIME
1 '2017-07-11 15:02:13'
1 '2017-07-11 15:20:28'
2 '2017-07-11 15:20:31'
2 '2017-07-11 15:21:13'
3 '2017-07-11 15:21:18'
3 '2017-07-11 15:21:21'
2 '2017-07-11 15:21:25'
2 '2017-07-11 15:21:59'
1 '2017-07-11 15:22:02'
1 '2017-07-11 15:22:05'",
header=TRUE,stringsAsFactors=FALSE)
df$LOCAL.DATETIME <- as.POSIXct(df$LOCAL.DATETIME)
library(dplyr)
df%>%
mutate(grp=data.table::rleid(POI))%>%
group_by(grp)%>%
summarise(POI=max(POI),TOTAL.TIME=difftime(max(LOCAL.DATETIME),
min(LOCAL.DATETIME),units="secs"))%>%
group_by(POI)%>%
summarise(TOTAL.TIME=sum(TOTAL.TIME))
# A tibble: 3 × 2
POI TOTAL.TIME
<int> <time>
1 1 1098 secs
2 2 76 secs
3 3 3 secs
要获得分钟和秒数,您可以使用as.period
中的lubridate
:
library(lubridate)
df%>%
mutate(grp=data.table::rleid(POI))%>%
group_by(grp)%>%
summarise(POI=max(POI),TOTAL.TIME=difftime(max(LOCAL.DATETIME),
min(LOCAL.DATETIME),units="secs"))%>%
group_by(POI)%>%
summarise(TOTAL.TIME=sum(TOTAL.TIME))%>%
mutate(TOTAL.TIME =as.period((TOTAL.TIME), unit = "sec"))
POI TOTAL.TIME
<int> <S4: Period>
1 1 18M 18S
2 2 1M 16S
3 3 3S
答案 1 :(得分:0)
另一个data.table
选项是为每个POI
创建2行的分组,计算它们之间的时差,最后将其总结为POI
:
library(data.table)
dt <- as.data.table(df)
dt[, grp2 := (seq_len(.N)+1) %/% 2, by = POI]
dt[, time_diff := difftime(LOCAL.DATETIME, shift(LOCAL.DATETIME), unit = "min"), by = .(POI, grp2)]
dt[ , .(TOTAL.TIME = sum(time_diff, na.rm = T)), by = POI]
# POI TOTAL.TIME
#1: 1 18.300000 mins
#2: 2 1.266667 mins
#3: 3 0.050000 mins