问题
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
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
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.
回答1:
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
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
回答2:
Another data.table
option is to create groupings of 2 rows for each POI
, take the time difference between them, and finally sum it up by 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
来源:https://stackoverflow.com/questions/45089710/calculating-cumulative-time-in-r