0

Im trying to introduce a weekly sliding time-window into my dataset (i.e., days 1-7 then 2-8, 3-9, 4-10... and so on). I currently have non-overlapping weekly time windows in my code but can't figure out how to repeat the data (each =7) but apply a +1 shift.

This gives me a non-overlapping weekly time window, but i need to repeat the dataset, extending the df having 1-7,2-8,3-9... consecutively.

library(EpiEstim)
data(Flu2009)

data_infer <- data.frame(t = 1:length(Flu2009$incidence$I) , 
                         I = c(Flu2009$incidence$I),
                         OI = overall_infectivity(Flu2009$incidence, Flu2009$si_distr) )

data_infer$logOI <- log(data_infer$OI)


data_infer$Rt <- ceiling(nrow(data_infer)/7)
data_infer$Rt[ 1:(floor(nrow(data_infer)/7)*7) ] <- rep(1:floor(nrow(data_infer)/7),each = 7)

Any help or guidance greatly appreciated.

  • Please post a [minimal reproducible example](https://stackoverflow.com/a/5963610/9462095) and an expected output. – Andre Wildberg Jun 19 '23 at 22:46
  • You should be looking at the zoo package. It has sliding windows for a variety of functions. – IRTFM Jun 20 '23 at 06:09
  • @AndreWildberg I've included the code that i'm working with if that makes the issue any clearer – Louis Wood Jun 20 '23 at 09:53
  • @IRTFM i've tried using the rollapply function from zoo package but was unsuccessful – Louis Wood Jun 20 '23 at 09:53
  • 2
    @Louis Wood can you please add an example of the expected output? Are you adding a column or columns with a 7-day moving average, or summarizing the data in some other way? – Seth Jun 20 '23 at 13:05

0 Answers0