R: Rolling window function with adjustable window and step-size for irregularly spaced observations

后端 未结 5 2083
萌比男神i
萌比男神i 2020-12-30 11:34

Say there is a 2-column data frame with a time or distance column which sequentially increases and an observation column which may have NAs here and there. How can I effici

5条回答
  •  再見小時候
    2020-12-30 12:17

    Here is a function that gives the same result for your small data frame. It's not particularly quick: it takes several seconds to run on one of the larger datasets in your second dat example.

    rolling_summary <- function(DF, time_col, fun, window_size, step_size, min_window=min(DF[, time_col])) {
        # time_col is name of time column
        # fun is function to apply to the subsetted data frames
        # min_window is the start time of the earliest window
    
        times <- DF[, time_col]
    
        # window_starts is a vector of the windows' minimum times
        window_starts <- seq(from=min_window, to=max(times), by=step_size)
    
        # The i-th element of window_rows is a vector that tells us the row numbers of
        # the data-frame rows that are present in window i 
        window_rows <- lapply(window_starts, function(x) { which(times>=x & times

提交回复
热议问题