Data
My original data frame contains information on lane changes by different drivers. Each driver changes lanes multiple times. I created a column lane.change that contains yes at the point where vehicle lane changes. Following is a sample data frame that contains 2 lane changes for a single driver:
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"))
Plot of lane changes:
The LC1 and LC2 lines show the range of lane changes in these data.
What I want to do:
I want to label the range of values that are shown in the plot. This represents the complete duration of a lane change. So, my desired output is:
Desired Output:
> 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
What I tried and problems:
I know that I can refer to relevant frames using x[which(x$lane.change=="yes"),"frames"]. But the goal is to label previous and next rows for each lane change. I am stuck on how to do that. In addition, I want to apply it to all drivers (in the original data), each having different number of lane changes (>=2). Please guide me o what function to use. I prefer using dplyr and purrr. Thanks in advance.
