Following is my sample data set:
> dput(lanec)
structure(list(vehicle.id = c(2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L,
5L, 5L), frame.id = c(1L, 2L, 3L, 4L, 5L, 3L, 4L, 5L, 6L, 7L,
8L, 9L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 6L, 7L, 8L, 9L, 10L, 11L,
12L), lane.change = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 1L,
2L, 1L), .Label = c(".", "yes"), class = "factor")), .Names = c("vehicle.id",
"frame.id", "lane.change"), class = "data.frame", row.names = c(NA,
-26L))
The first column is the IDs of vehicles entering on a particular segment of a freeway. They were observed until they left the segment so each vehicle has different number of time frames in which it was observed. The frame numbers are given in frame.id
column. The third column tells whether the vehicle changed the lane and in which frame.
In this sample data all except vehicle # 2 changed the lane. Vehicle # 5 changed the lane twice.
Required
I want to identify which vehicles changed the lane and remove them from data set. I tried using subset(lanec, lane.change!='yes')
but it only remove those rows where the value of lane.change
is yes
. Using the sample data set, the desired output should be:
vehicle.id frame.id lane.change
1 2 1 .
2 2 2 .
3 2 3 .
4 2 4 .
5 2 5 .
How can I achieve this? It must be simple but I can't figure it out. Thanks in advance.