Ant
Ant

Reputation: 820

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

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!

Upvotes: 1

Views: 87

Answers (1)

akrun
akrun

Reputation: 887731

 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

Upvotes: 1

Related Questions