Time series of the subset so that the selected series differ by a certain minimum time

I use a data table in R to store time series. I want to return a subset so that consecutive lines for the selected times are at least N seconds from the last selected line, for example. if I have

library(data.table)
x <- data.table(t=c(0,1,3,4,5,6,7,10,16,17,18,20,21), v=1:13)
x
     t  v
 1:  0  1
 2:  1  2
 3:  3  3
 4:  4  4
 5:  5  5
 6:  6  6
 7:  7  7
 8: 10  8
 9: 16  9
10: 17 10
11: 18 11
12: 20 12
13: 21 13

and I want to try rows that are at least 5 seconds apart, starting from the first row, then I should get a data table with time / value pairs:

y <- x[...something...]
y
     t  v
 1:  0  1
 2:  5  5
 3: 10  8
 4: 16  9
 5: 21 13

Samples of time also should not be on a regular basis, so I can’t just take all M lines. Of course, I could do this by manually sorting the lines of data.table, but I'm wondering if there is a more convenient way to express this using indexing data.tables.

+4
1

w :

t_plus = 5

# one join per row visited
w   <- c()
nxt <- 1L
while(!is.na(nxt)){ 
  w   <- c(w, nxt) 
  nxt <- x[.(t[nxt]+t_plus), on=.(t), roll=-Inf, which=TRUE]
}

# join once on all rows
w0  <- x[.(t+5), on=.(t), roll=-Inf, which=TRUE]

w   <- c()
nxt <- 1L
while (!is.na(nxt)){ 
  w   <- c(w, nxt)
  nxt <- w0[nxt] 
}

x[w].


, , OP " 5 "; , .

@DavidArenburg Q & A Henrik, . , , .

, R ( w ). , .

+4

Source: https://habr.com/ru/post/1667590/


All Articles