【发布时间】:2013-12-03 14:01:23
【问题描述】:
我有一个每 0.1 秒记录一次的大型车辆数据集,如下所示:
id frame lane class
1 2 13 1 1
2 2 14 1 1
3 2 15 2 1
4 2 16 2 1
5 4 18 3 3
6 4 19 3 3
7 4 20 3 3
8 5 15 2 2
9 5 16 2 2
10 5 17 2 2
11 5 18 3 2
12 5 19 3 2
13 6 14 1 3
14 6 15 1 3
15 6 16 1 3
16 6 17 2 3
17 6 18 2 3
'frame'为录像帧ID,'lane'为车辆占用的车道#,'class'为车辆分类,即1=摩托车,2=汽车,3=卡车。
需要的输出
我想在以下四列中查找第一次和最后一次出现的车辆 ID 及其相关数据:
id
Origin (lane # in the first occurrence of id)
Destination (lane # in the last occurrence of id)
class
到目前为止我所做的尝试:
(注意输入表是“输入”数据框)
input$first <- !duplicated(input$'id')
input$last <- !duplicated(input$'id', fromLast=T)
ODTable <- subset(input, m$'first'==T | m$'last'==T)
我得到了以下输出,它给了我正确的信息,但不是所需的格式:
ODTable
id frame lane class first last
1 2 13 1 1 TRUE FALSE
4 2 16 2 1 FALSE TRUE
5 4 18 3 3 TRUE FALSE
7 4 20 3 3 FALSE TRUE
8 5 15 2 2 TRUE FALSE
12 5 19 3 2 FALSE TRUE
13 6 14 1 3 TRUE FALSE
17 6 18 2 3 FALSE TRUE
【问题讨论】:
-
您可能要考虑在此处使用
data.table,因为它可以更清晰地过滤您的数据。与shiny也一样
标签: r