Rolling sums for groups with uneven time gaps

前端 未结 6 1363
说谎
说谎 2021-02-16 00:19

Here\'s the tweak to my previously posted question. Here\'s my data:

set.seed(3737)
DF2 = data.frame(user_id = c(rep(27, 7), rep(11, 7)),
            date = as.D         


        
6条回答
  •  遥遥无期
    2021-02-16 00:34

    You can use rollapply from zoo once you fill out the missing dates first:

    library(dplyr)
    library(zoo)
    
    set.seed(3737)
    DF2 = data.frame(user_id = c(rep(27, 7), rep(11, 7)),
                 date = as.Date(rep(c('2016-01-01', '2016-01-03', '2016-01-05', '2016-01-07', '2016-01-10', '2016-01-14', '2016-01-16'), 2)),
                 value = round(rnorm(14, 15, 5), 1))
    
    all_combinations <- expand.grid(user_id=unique(DF2$user_id), 
                                date=seq(min(DF2$date), max(DF2$date), by="day"))
    
    res <- DF2 %>% 
        merge(all_combinations, by=c('user_id','date'), all=TRUE) %>%
        group_by(user_id) %>% 
        arrange(date) %>% 
        mutate(v_minus7=rollapply(value, width=8, FUN=function(x) sum(x, na.rm=TRUE), partial=TRUE, align='right'),
               v_minus14=rollapply(value, width=15, FUN=function(x) sum(x, na.rm=TRUE), partial=TRUE, align='right')) %>%
        filter(!is.na(value))
    

提交回复
热议问题