【发布时间】:2017-06-29 14:58:06
【问题描述】:
数据
我的原始数据框包含有关不同驾驶员变道的信息。每个司机多次变换车道。我创建了一个列lane.change,其中包含yes 在车道改变的点。以下是一个示例数据帧,其中包含单个驾驶员的 2 次车道变换:
x <- structure(list(file.ID = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L), class = "factor", .Label = "Car1"), frames = 1:11,
lane.change = structure(c(1L, 1L, 1L, 2L, 1L, 1L, 1L, 2L,
1L, 1L, 1L), .Label = c("no", "yes"), class = "factor"),
y.m = c(80, 80, 80, 81, 82, 82, 82, 83, 84, 84, 84)), row.names = c(NA,
-11L), class = "data.frame", .Names = c("file.ID", "frames",
"lane.change", "y.m"))
变道图:
LC1 和 LC2 行显示了这些数据中的车道变化范围。
我想做什么:
我想标记图中显示的值范围。这表示变道的完整持续时间。所以,我想要的输出是:
期望的输出:
> x
file.ID frames lane.change range_LC y.m
1 Car1 1 no . 80
2 Car1 2 no . 80
3 Car1 3 no LC1 80
4 Car1 4 yes LC1 81
5 Car1 5 no LC1 82
6 Car1 6 no . 82
7 Car1 7 no LC2 82
8 Car1 8 yes LC2 83
9 Car1 9 no LC2 84
10 Car1 10 no . 84
11 Car1 11 no . 84
我的尝试和问题:
我知道我可以使用x[which(x$lane.change=="yes"),"frames"] 引用相关的frames。但目标是为每次换道标记上一行和下一行。我被困在如何做到这一点上。另外,我想将它应用于所有司机(在原始数据中),每个司机都有不同的车道改变次数(>=2)。请指导我使用什么功能。我更喜欢使用dplyr 和purrr。提前致谢。
【问题讨论】:
标签: r