How do I do a conditional sum which only looks between certain date criteria

前端 未结 7 484
遥遥无期
遥遥无期 2020-12-18 13:47

Say I have data that looks like

date, user, items_bought, event_number
2013-01-01, x, 2, 1
2013-01-02, x, 1, 2
2013-01-03, x, 0, 3
2013-01-04, x, 0, 4
2013-0         


        
7条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-18 13:57

    The following looks valid:

    unlist(lapply(split(data, data$user), 
                  function(x) {
                     ave(x$items_bought, 
                     cumsum(c(0, diff(x$date)) >= 3), FUN = cumsum) 
                  }))   
    #x1  x2  x3  x4  y1  y2  y3  y4 
    # 2   3   3   4   1   6   6   7
    

    Where data:

    data = structure(list(date = structure(c(15706, 15707, 15710, 15711, 
    15706, 15707, 15710, 15711), class = "Date"), user = structure(c(1L, 
    1L, 1L, 1L, 2L, 2L, 2L, 2L), .Label = c(" x", " y"), class = "factor"), 
        items_bought = c(2L, 1L, 3L, 1L, 1L, 5L, 6L, 1L)), .Names = c("date", 
    "user", "items_bought"), row.names = c(NA, -8L), class = "data.frame")
    

提交回复
热议问题