r calculating rolling average with window based on value (not number of rows or date/time variable)

前端 未结 4 1165
小蘑菇
小蘑菇 2021-01-18 05:55

I\'m quite new to all the packages meant for calculating rolling averages in R and I hope you can show me in the right direction.

I have the following data as an exa

4条回答
  •  渐次进展
    2021-01-18 06:44

    Try out:

    library(dplyr)
    
    # count the number of values per ms
    df <- df %>%
            group_by(ms) %>%
            mutate(Nb.values = n())
    
    # consider a window of 1 ms and compute the percentage for each window
    df2 <- setNames(aggregate(correct ~ factor(df$ms, levels = as.character(seq(min(df$ms), max(df$ms), 1))),
                              df, sum),
                    c("ms", "Count.correct"))
    
    # complete data frame (including unused levels)
    df2 <- tidyr::complete(df2, ms)
    df2$ms <- as.numeric(levels(df2$ms))[df2$ms]
    df2 <- df2 %>% left_join(distinct(df[, c(1, 3)]), "ms")
    
    # compute a rolling mean of the percentage of correct, with a width of 5
    df2 %>%
            mutate(Window = paste(ms, ms+4, sep = "-"), # add windows
                   Rolling.correct = zoo::rollapply(Count.correct, 5, sum, na.rm = T,
                                                    partial = TRUE, fill = NA, align = "left") /
                           zoo::rollapply(Nb.values, 5, sum, na.rm = T, partial = TRUE,
                                          fill = NA, align = "left")) # add rolling mean
    
    # A tibble: 43 x 5
          ms Count.correct Nb.values  Window Rolling.correct
                                   
     1   300             2         3 300-304            0.40
     2   301             0         1 301-305            0.00
     3   302            NA        NA 302-306            0.25
     4   303             0         1 303-307            0.25
     5   304            NA        NA 304-308            0.25
     6   305             0         2 305-309            0.25
     7   306             1         1 306-310            0.25
     8   307            NA        NA 307-311            0.00
     9   308             0         1 308-312            0.20
    10   309            NA        NA 309-313            0.25
    # ... with 33 more rows
    

提交回复
热议问题