如何解决确定时间序列中的中断,并为R中的每个中断分配唯一因子
我有每百艘船很大的船径时间序列数据。时间序列是多年的,因此每艘船都有多个航迹。每个“航迹”都是每小时的数据,在时间序列中有很大的间隔(>天),我希望用来识别每艘船的每个航迹。
我的计划是使用循环,首先选择船只及其整个时间序列,然后确定每艘船只的唯一航迹,然后将每条所选船只的单独航迹拆分(成列表),然后做一些数学运算(未拆分,并附加到所有容器的新数据框。我无法解决如何为split()的每个已确定轨道赋予唯一因素。一些简化的数据是;
vessel<-c(rep("A",11))
time <- as.POSIXct(c("2017-01-01 00:02:25 GMT","2017-01-01 01:31:26 GMT","2017-01-01 02:37:42 GMT","2017-01-01 03:14:34 GMT","2017-01-01 04:09:45 GMT","2017-02-01 05:51:53 GMT","2017-03-01 06:22:24 GMT","2017-03-01 07:34:44 GMT","2017-03-01 08:01:15 GMT","2017-03-01 09:16:44 GMT","2017-03-01 10:48:12 GMT"))
df<-data.frame(vessel,time)
您会看到我添加了一个不属于轨道的时间(第6行)-数据充满了不属于任何轨道的单个ping。我还想知道如何处理这些事件并以某种方式将其删除。到目前为止,我在其他帖子中获得的代码看起来像这样;
df$gap <- c(0,with(df,time[-1] - time[-nrow(df)]))/60 # results in hours between rows
gap_threshold <- 10 # anything greater that 10 hours difference I treat as a different track
df$over_thresh <- df$gap < gap_threshold
df
这可以确定中断的位置,但是如何为split(df,df$split_factor)
中可以使用的每个中断分配唯一的因子?理想情况下,最终df应该看起来像,但我不知道如何创建列“ split_factor”?
vessel time gap over_thresh split_factor
1 A 2017-01-01 00:02:25 0.0000000 TRUE split_1
2 A 2017-01-01 01:31:26 1.4836111 TRUE split_1
3 A 2017-01-01 02:37:42 1.1044444 TRUE split_1
4 A 2017-01-01 03:14:34 0.6144444 TRUE split_1
5 A 2017-01-01 04:09:45 0.9197222 TRUE split_1
6 A 2017-02-01 05:51:53 745.7022222 FALSE delete
7 A 2017-03-01 06:22:24 672.5086111 FALSE split_2
8 A 2017-03-01 07:34:44 1.2055556 TRUE split_2
9 A 2017-03-01 08:01:15 0.4419444 TRUE split_2
10 A 2017-03-01 09:16:44 1.2580556 TRUE split_2
11 A 2017-03-01 10:48:12 1.5244444 TRUE split_2
>
第二条轨道从第7行开始,但由于与前一行的时间不同,因此被标识为FALSE。但是,需要将其标记为下一首曲目的一部分。
而且,这都是通过空间数据帧完成的,所以我认为可以做到这一点,但我可能是错的。我可以提取数据,然后重新创建空间数据框,没问题。谢谢。
解决方法
这里是data.table
的一个选项。根据'over_thresh',通过'vessel'分组,使用rleid
创建分组索引,然后将'split_factor'创建为带有'delete'字符串的列。获取“ over_thresh”中按{vessel”,“ grp”分组的“ {_1}} .I
个元素所在的行的索引(any
,在TRUE
中使用它,得到组索引(i
)并粘贴子字符串.GRP
,将split
中的行元素分配给'split_factor'
i
或使用library(data.table)
setDT(df)[,grp := rleid(over_thresh|shift(over_thresh,type = 'lead')),vessel]
df[,split_factor := 'delete']
i1 <- df[,.I[any(over_thresh)],.(vessel,grp)]$V1
df[i1,split_factor := paste0('split_',.GRP),grp)][,grp := NULL][]
# vessel time gap over_thresh split_factor
# 1: A 2017-01-01 00:02:25 0.0000000 TRUE split_1
# 2: A 2017-01-01 01:31:26 1.4836111 TRUE split_1
# 3: A 2017-01-01 02:37:42 1.1044444 TRUE split_1
# 4: A 2017-01-01 03:14:34 0.6144444 TRUE split_1
# 5: A 2017-01-01 04:09:45 0.9197222 TRUE split_1
# 6: A 2017-02-01 05:51:53 745.7022222 FALSE delete
# 7: A 2017-03-01 06:22:24 672.5086111 FALSE split_2
# 8: A 2017-03-01 07:34:44 1.2055556 TRUE split_2
# 9: A 2017-03-01 08:01:15 0.4419444 TRUE split_2
#10: A 2017-03-01 09:16:44 1.2580556 TRUE split_2
#11: A 2017-03-01 10:48:12 1.5244444 TRUE split_2
和dplyr
,在按“容器”分组后,在“ over_thresh”上使用rle
或(rle
)|
(即下一个值)“ over_thresh”,它返回lead
和list
的{{1}}。现在,我们通过将“值”的序列“ split_”分配为TRUE,然后将lengths
的值更改为'删除'
values
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。