Retaining the previous date in R

2019-07-09 09:40发布

问题:

I got stuck at a fairly easy data munging task. I have a transactional data frame in R that resembles this one:

id<-c(11,11,22,22,22)
dates<-as.Date(c('2013-11-15','2013-11-16','2013-11-15','2013-11-16','2013-11-17'), "%Y-%m-%d")
example<-data.frame(id=id,dates=dates)

  id      dates
1 11 2013-11-15
2 11 2013-11-16
3 22 2013-11-15
4 22 2013-11-16
5 22 2013-11-17

I'm looking for a way to retain the date of the previous transaction. The resulting table would look like this:

previous_dates<-as.Date(c('','2013-11-15','','2013-11-15','2013-11-16'), "%Y-%m-%d")
example2<-data.frame(id=id,dates=dates, previous_dates=previous_dates)

  id      dates previous_dates
1 11 2013-11-15           <NA>
2 11 2013-11-16     2013-11-15
3 22 2013-11-15           <NA>
4 22 2013-11-16     2013-11-15
5 22 2013-11-17     2013-11-16

I looked into other similar problems and one solution that is very close to what I want is:

library(data.table)
dt <- as.data.table(example)

prev_date <- function(x) c(x[1],x)

dt[,prev:=prev_date(dates), by=id]

The problem with this one is that if there is no previous date (like in the case of id=11 dates=2013-11-15) the function would output the same date resulting in:

  id      dates previous_dates
1 11 2013-11-15     2013-11-15
2 11 2013-11-16     2013-11-15

Can someone help, please?

回答1:

example$previous_dates <- ave(example$dates, example$id,
                 FUN=  function(dt) c.Date( c(NA, dt[-length(dt)])
                                              ))
> example
  id      dates previous_dates
1 11 2013-11-15           <NA>
2 11 2013-11-16     2013-11-15
3 22 2013-11-15           <NA>
4 22 2013-11-16     2013-11-15
5 22 2013-11-17     2013-11-16

Playing around with the classes of Date objects .... this also works:

example$previous_dates <- ave(example$dates, example$id,
             FUN=  function(dt) structure( 
                                  c(NA, dt[-length(dt)]),
                                  class="Date" )  )


回答2:

library(plyr)
example <- ddply(example, .(id), transform, 
                                  previous_dates=c(as.Date(NA), head(dates, -1)))
  id      dates previous_dates
1 11 2013-11-15           <NA>
2 11 2013-11-16     2013-11-15
3 22 2013-11-15           <NA>
4 22 2013-11-16     2013-11-15
5 22 2013-11-17     2013-11-16


回答3:

Just another approach:

transform(example, previous_dates = ave(dates, id, FUN = 
                                         function(x) x[c(NA, (seq_along(x)-1))]))

  id      dates previous_dates
1 11 2013-11-15           <NA>
2 11 2013-11-16     2013-11-15
3 22 2013-11-15           <NA>
4 22 2013-11-16     2013-11-15
5 22 2013-11-17     2013-11-16