R - Number of days since last occurrence

前端 未结 2 415
心在旅途
心在旅途 2021-01-24 06:18

I have a data frame that looks like this:

 id     date
 1001   2012-10-11
 1005   2013-02-20
 1005   2012-11-21
 1005   2014-03-14
 1003   2013-10-25
 1003   201         


        
2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-24 06:43

    Or using ave (similar to @David Arenburg's approach)

     indx <- with(df, order(id, date))
     df1 <- transform(df[indx,], no_of_days=ave(as.numeric(date), id,
                        FUN= function(x) c(NA, diff(x))))[order(indx),]
     df1     
     #    id       date no_of_days
     #1 1001 2012-10-11         NA
     #2 1005 2013-02-20         91
     #3 1005 2012-11-21         NA
     #4 1005 2014-03-14        387
     #5 1003 2013-10-25         NA
     #6 1003 2013-11-30         36
    

    data

    df <- structure(list(id = c(1001L, 1005L, 1005L, 1005L, 1003L, 1003L
    ), date = structure(c(15624, 15756, 15665, 16143, 16003, 16039
    ), class = "Date")), .Names = c("id", "date"), row.names = c(NA, 
    -6L), class = "data.frame")
    

提交回复
热议问题