本文介绍了创建一个在其他规则增加的行中的每个间隙后增加的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 samples 数据框,其中包含一些以规则间隔的时间戳(1 秒间隔)读取的数据.

I've got a samples data frame which contains some readings at regularily-spaced timestamps (1 sec. interval).

                      TS           Pressure     Temperature
[...]
8  2014-08-26 00:18:26.8                105              30
9  2014-08-26 00:18:27.8                108              32
10 2014-08-26 00:18:28.8              109.9              31
11 2014-08-26 00:34:20.8                109              20
12 2014-08-26 00:34:21.8                100              24
13 2014-08-26 00:34:22.8                 95              22
[...]

我只有在某些感兴趣的事件(例如,当 Pressure )期间有记录,并且在这些事件之外没有任何记录.

I only have records during some events of interest (e.g. when Pressure < 110) and don't have any records outside of these events.

我想给每个时期"一个唯一的 ID,其中 Pressure
我制作了另一个数据框 EventBoundaryIndex ,其中包含每个时期的边界索引和相应的 ID 号:

I want to give an unique ID to each "period" where Pressure < 110
I've made another data frame EventBoundaryIndex which holds the boundary indices of each period and the corresponding ID number:

> EventBoundaryIndex
     fromIndex  toIndex  eventID
1            1       10        1
2           11       30        2
[...]

阅读:事件#1 应该跨越samples[0:10, ],事件#2 跨越samples[11:30, ]
现在,我想在原始 samples 数据框中添加一个 eventID 行,以指示每个记录属于哪个 eventID:

read: event #1 should span from samples[0:10, ], event #2 spans samples[11:30, ], etc.
Now I would like to add an eventID row to my original samples data frame to indicate which eventID each record belongs to:

                      TS           Pressure     Temperature   EventID
[...]
8  2014-08-26 00:18:26.8                105              30         1
9  2014-08-26 00:18:27.8                108              32         1
10 2014-08-26 00:18:28.8              109.9              31         1
11 2014-08-26 00:34:20.8                109              20         2
12 2014-08-26 00:34:21.8                100              24         2
13 2014-08-26 00:34:22.8                 95              22         2
[...]

我试过了:

samples$eventID[EventBoundaryIndex$from : EventBoundaryIndex$to] 
        <- EventBoundaryIndex$eventID

但这不起作用.
我想我需要某种形式的 apply 但找不到正确的方法.

but that doesn't work.
I guess I need some form of apply but can't figure out the right way.


如果您能想到一种更简单的方法,每次在两个连续时间戳之间检测到超过 1 秒的间隔"时,我的 eventID 索引都会增加,请告诉我!

Or
If you can think of a simpler way to have my eventID index increasing each time a "gap" of more than 1 sec is detected bewteen two consecutive timestamps, let me know!

推荐答案

 samples$eventID <- cumsum(c(TRUE,diff(as.POSIXct(samples$TS))>1))
 samples
 #                      TS Pressure Temperature eventID
 #8  2014-08-26 00:18:26.8    105.0          30       1
 #9  2014-08-26 00:18:27.8    108.0          32       1
 #10 2014-08-26 00:18:28.8    109.9          31       1
 #11 2014-08-26 00:34:20.8    109.0          20       2
 #12 2014-08-26 00:34:21.8    100.0          24       2
 #13 2014-08-26 00:34:22.8     95.0          22       2

这篇关于创建一个在其他规则增加的行中的每个间隙后增加的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 12:26