0

A (small example) of my data is structured as follows:

Individ <- data.frame(Participant = c("Bill", "Bill", "Bill", "Bill", "Bill", "Bill", "Jane", "Jane", "Jane", "Jane", 
                                      "Jane", "Jane", "Jane", "Jane", "Jane", "Jane", "Jane", "Jane", "Bill", "Bill", "Bill", "Bill", "Bill", "Bill"),  
                      Time = c(1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6),
                      Condition = c("Placebo", "Placebo", "Placebo", "Placebo", "Placebo", "Placebo", "Expr", "Expr", "Expr", "Expr", "Expr", "Expr", 
                                    "Placebo", "Placebo", "Placebo", "Placebo", "Placebo", "Placebo", "Expr", "Expr", "Expr", "Expr", "Expr", "Expr"),
                      Location = c("Home", "Home", "Home", "Home", "Home", "Home", "Home", "Home", "Home", "Home", "Home", "Home", "Home", "Home", "Home", "Home", "Home", "Home", 
                                   "Away", "Away", "Away", "Away", "Away", "Away"),
                      Power = c(400, 250, 180, 500, 300, 450, 600, 512, 300, 500, 450, 200, 402, 210, 130, 520, 310, 451, 608, 582, 390, 570, 456, 205))

I wish to calculating a rolling three second average of Power over Time for each Participant at both Condition and Location.

My research design is a repeated measures cross-over, evident that Jane has power recorded at Home under both treatment conditions (Placebo v Exper). My dataset is 4 million rows in length, how can I please quickly and easily calculate this moving average?

This question: Constructing moving average over a categorical variable in R is similar as is this: Rolling mean (moving average) by group/id with dplyr but I am wanting to average over a three second period and more than one grouping variable.

Community
  • 1
  • 1
user2716568
  • 1,866
  • 3
  • 23
  • 38
  • Having another group doesn't really change the question. Take the [lovely data.table answer you linked to](http://stackoverflow.com/a/26205144/903061), and where it has `n = 2`, you use `n = 3` since you want a 3-second period, and where they have `by = ID`, you use `by = .(Participant, Condition, Location)`. – Gregor Thomas Feb 13 '16 at 08:20
  • @Gregor Thank you, apologies for the duplicate. – user2716568 Feb 13 '16 at 08:29
  • No worries! If you have trouble getting it to work, feel free to update your question here with your attempt and we can re-open the question if necessary to help you figure out what about the other solution isn't working. – Gregor Thomas Feb 13 '16 at 09:34

0 Answers0