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

前端 未结 4 1125
小蘑菇
小蘑菇 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:34

    This could be done with base R:

    calculate_irregular_ratio <- function(df, time_var = "ms", window_var = 5, calc_var = "correct") {
    
    sapply(df[[time_var]], function(x) round(mean(df[[calc_var]][df[[time_var]] >= (x - window_var) & df[[time_var]] <= x]), 2))
    
    }
    

    You can apply it as follows (the default is set to 5 ms, you can change it with changing the window_var parameter):

    df$window_5_ratio <- calculate_irregular_ratio(df, window_var = 5)
    

    In your case, you would get (first 10 rows shown only):

        ms correct window_5_ratio
    1  300       1           0.67
    2  300       1           0.67
    3  300       0           0.67
    4  301       0           0.50
    5  303       0           0.40
    6  305       0           0.29
    7  305       0           0.29
    8  306       1           0.20
    9  308       0           0.20
    10 310       0           0.17
    

    It behaves like a rolling mean, however it does not rely on rows. Instead, it takes the window based on values in a column.

    For instance, at rows 6 and 7, it takes the value of current row (305 ms), and calculates the ratio on all the values in dataframe that are 305 and - 5, i.e. between 305 and 300, yielding 0.29.

    You can of course always modify the function yourself, e.g. if you'd like window 5 to actually mean 301 - 305 and not 300 - 305, you can set + 1 after x - window_var, etc.

提交回复
热议问题