r - 下一个出现的记录的索引

标签 r dplyr data.table

我有一个自行车轨迹的样本数据集。我的目标是计算出两次访问 B 站之间的平均时间间隔。

到目前为止,我已经能够简单地通过以下方式订购数据集:

test[order(test$starttime, decreasing = FALSE),]

并找到 start_stationend_station 等于 B 的行索引。

 which(test$start_station == 'B')
 which(test$end_station == 'B')

下一部分是我遇到麻烦的地方。为了计算自行车到达 B 站之间的时间间隔,我们必须在 start_station = "B"(自行车离开)之间获取 difftime()以及下一个出现的记录,其中 end_station= "B"即使该记录恰好位于同一行(参见第 6 行)。

根据下面的数据集,我们知道自行车在 7:30:0016:00:00 之间在 B 站外行驶了 510 分钟,中间间隔了 30 分钟B站外18:00:0018:30:00,以及19:00:00之间210分钟22:30:00 B站外,平均250分钟。

如何使用 difftime() 在 R 中重现这一输出?

> test
   bikeid start_station           starttime end_station             endtime
1       1             A 2017-09-25 01:00:00           B 2017-09-25 01:30:00
2       1             B 2017-09-25 07:30:00           C 2017-09-25 08:00:00
3       1             C 2017-09-25 10:00:00           A 2017-09-25 10:30:00
4       1             A 2017-09-25 13:00:00           C 2017-09-25 13:30:00
5       1             C 2017-09-25 15:30:00           B 2017-09-25 16:00:00
6       1             B 2017-09-25 18:00:00           B 2017-09-25 18:30:00
7       1             B 2017-09-25 19:00:00           A 2017-09-25 19:30:00
8       1             А 2017-09-25 20:00:00           C 2017-09-25 20:30:00
9       1             C 2017-09-25 22:00:00           B 2017-09-25 22:30:00
10      1             B 2017-09-25 23:00:00           C 2017-09-25 23:30:00

这里是示例数据:

> dput(test)
structure(list(bikeid = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1), start_station = c("A", 
"B", "C", "A", "C", "B", "B", "А", "C", "B"), starttime = structure(c(1506315600, 
1506339000, 1506348000, 1506358800, 1506367800, 1506376800, 1506380400, 
1506384000, 1506391200, 1506394800), class = c("POSIXct", "POSIXt"
), tzone = ""), end_station = c("B", "C", "A", "C", "B", "B", 
"A", "C", "B", "C"), endtime = structure(c(1506317400, 1506340800, 
1506349800, 1506360600, 1506369600, 1506378600, 1506382200, 1506385800, 
1506393000, 1506396600), class = c("POSIXct", "POSIXt"), tzone = "")), .Names = c("bikeid", 
"start_station", "starttime", "end_station", "endtime"), row.names = c(NA, 
-10L), class = "data.frame")

最佳答案

这将按照出现的顺序计算差异,但不会将其附加到data.frame

lapply(df1$starttime[df1$start_station == "B"], function(x, et) difftime(et[x < et][1], x, units = "mins"), et = df1$endtime[df1$end_station == "B"])

[[1]]
Time difference of 510 mins

[[2]]
Time difference of 30 mins

[[3]]
Time difference of 210 mins

[[4]]
Time difference of NA mins

计算平均时间:

v1 <- sapply(df1$starttime[df1$start_station == "B"], function(x, et) difftime(et[x < et][1], x, units = "mins"), et = df1$endtime[df1$end_station == "B"])
mean(v1, na.rm = TRUE)

[1] 250

关于r - 下一个出现的记录的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46410988/

相关文章:

使用名为 'x' : 的变量时更新公式时出现 R 错误

r - 使用聚合操作时如何避免在 data.table 中创建重复项

r - fread 读取错误 "Expected sep (' ') but.."

r - 在 R 中使用 ggplot2 表达三个变量之间的关系

r - 修复多个绘图中的 ggplot 填充比例

html - 提高 R 代码有效性的技巧

R: dplyr 管道条件超前/滞后使用 ifelse 具有意外行为

dplyr 的相对频率/比例

r - 使用 ggplot 以编程方式绘制热门事件的子事件 : R

r - data.table 按名称引用另一个 data.table 中的列