Create an index that increases after each gap in otherwise regularily-increasing row

…衆ロ難τιáo~ 提交于 2020-01-30 08:15:13

问题


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
[...]

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

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
[...]

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
[...]

I tried:

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

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

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!


回答1:


 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


来源:https://stackoverflow.com/questions/25616252/create-an-index-that-increases-after-each-gap-in-otherwise-regularily-increasing

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!