Search code examples
rdatevectorlag

Create a lag vector with repeated entries


I would like to create a lagged vector, but the challenge is that some observations are repeated. Let's consider the following vector:

date <- c("2016-04-11", "2016-04-11", "2016-04-11", "2015-05-11", "2015-05-11", "2014-05-11")

The first three elements are observed at the same point in time (items of last order), the fourth and fifth (items of second last order) at the same time and the sixth at another time (first order, consisting only of one item).

The lagged vector should look like this:

date <- c("2015-05-11", "2015-05-11", "2015-05-11", "2014-05-11", "2014-05-11", NA)

Is there an easy way to create this lagged date vector?


Solution

  • Run-length encode the data:

    date <- c("2016-04-11", "2016-04-11", "2016-04-11", "2015-05-11", "2015-05-11", "2014-05-11")
    res <- rle(date)
    res$values <- c(res$values[-1], NA)
    res <- inverse.rle(res)
    #[1] "2015-05-11" "2015-05-11" "2015-05-11" "2014-05-11" "2014-05-11" NA