-2

I have a dataframe "data" where the column V11 contains the timestamp for each row. I want to add a sliding windows to select the appropriate rows respect the timestamp. The sliding windows has a length of 200 ms and it should move each 100 ms. However, these values may be user configurable. Thus, the first selection of rows would be from time 0 to 200ms, the next 100 to 300 ms, and so on. I have seen packages that the mean is calculated but I need to do my own calculations.

Any idea?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Ruser
  • 85
  • 1
  • 7

1 Answers1

1
apply_function_on_sliding_window <- function(data, col, start, end, fun) {
    range_start = data[col] >= start
    range_end = data[range_start, col] <= end
    return(fun(data[range_start, col][range_end])
}

apply_function_on_sliding_window(data, V11, start, end, mean)
Marcin Możejko
  • 39,542
  • 10
  • 109
  • 120
  • It works for a data frame type? I would like to obtain the selection of rows and this code does the mean, right? – Ruser Apr 11 '16 at 12:49
  • With data.frame - if this doesn't do the right thing - you may try add as.numeric to appropriate data row. – Marcin Możejko Apr 11 '16 at 13:26