tl;dr I need to condition if a promotion was on or not based upon drops(or not) in price over time. I am open to alternative approaches.

I have a data frame of prices split across several grouping factors over time. My goal is for each 'ITEM' in 'EACH' store to check the mode of the 'PRICE' for the past 7 dates (if they exist). If the value of the observation is less than 10% of the mode of price, then in the 'Promotion' column should be populated with a 1, if not a 0.

**EXAMPLE DATA**

```
dat <- data.frame(Date = sample(seq(as.Date('1999/01/01'), as.Date('2000/01/01'), by="day"), 10),
              Item = rep(LETTERS[1:4], times = 10),
              Store =  as.factor(sample(rep(c("NY","SYD","LON","PAR"), each = 10))),
              Price = rnorm(n = 40, mean = 2.5, sd = 1))

```

So far I have used dplyr's `group_split` to break out item and store groupings into separate data frames to capture all the conditions. What I believe I need to do now is `mutate` the new column using an `ifelse` statement with `rollapply`. I have so far attempted to use the following line of code...

```
data %>% mutate(Promotion = ifelse(rollapply(Price, 7, Mode <= Price*0.91,1,0)))

```

this returns an error statement...

```
Error: Problem with `mutate()` input `PRMT_IND2`.
x comparison (5) is possible only for atomic and list types
i Input `PRMT_IND2` is `ifelse(...)`.

```

I am not really sure where to go from here. If you have time I would also appreciate it if you could tell me how to apply this across all the groups created by the group\_split, and how to stitch this back together.

*note.* Observations (dates/rows) are no even across shops, and some are populated with less than 7 days. I can remove these if the rolling apply will not work without it. But that loses quite a chunk of data.

I am using this function for the Mode...

```
  Mode <- function(x) {
  ux <- unique(x)
  ux[which.max(tabulate(match(x, ux)))]
}

```