计算R中的累积时间 [英] Calculating cumulative time in R
问题描述
我有一个看起来像这样的数据框:
I have a dataframe that looks like this:
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上花费的累积时间,并将它们组合成一个看起来像这样的表:
I want to be able to calculate (probably with lubridate) the cumulative time spent at each POI and combine them into a table that looks something like this:
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行的时间.
Also, I am not sure how to deal with the time between POIs, like the 3 seconds between rows 2 and 3. I think maybe I need to calculate the time from row 1 to row 3 instead of row 1 to row 2.
推荐答案
要获取每个组时段中的总时间,首先需要创建一个组索引.我正在使用data.table
中的rleid
,然后,可以计算出每个组中花费的总时间,然后使用sum
通过初始POI进行汇总.
To get the total time in each group's periods, you first need to create a group index. I'm using rleid
from data.table
You can then, calculate the total time spent in each of these groups, and then summarise by the initial POI using sum
.
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
要获取分钟和秒钟,可以使用lubridate
中的as.period
:
To get minute and seconds, you can use as.period
from 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
这篇关于计算R中的累积时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!