Search code examples
rdataframedplyrlag

Create lag onto next group in R


Hi I would like to create a lag by one whole group on a dataframe in R. So lets say the value for the group A is 11 I would like to make a lag where all values of group B are 11 and so on. Below is an example of what I would like to do.

Letter = c('A', 'A', 'A', 'B', 'C', 'C', 'D')
Value = c(11, 11, 11, 4, 8, 8, 10)
data.frame(Letter, Value)

  Letter Value
1      A    11
2      A    11
3      A    11
4      B     4
5      C     8
6      C     8
7      D    10

And then have it become after the lag:

Lag = c(NA, NA, NA, 11, 4, 4, 8)
data.frame(Letter, Value, Lag)

 Letter Value Lag
1      A    11  NA
2      A    11  NA
3      A    11  NA
4      B     4  11
5      C     8   4
6      C     8   4
7      D    10   8

(One thing to note is all values of the group will be the same)


Solution

  • Get the unique rows of the data, lag Value and then left join the original data frame with that.

    library(dplyr)
    
    DF %>%
      left_join(mutate(distinct(.), Lag = lag(Value), Value = NULL), by = "Letter")
    

    giving:

      Letter Value Lag
    1      A    11  NA
    2      A    11  NA
    3      A    11  NA
    4      B     4  11
    5      C     8   4
    6      C     8   4
    7      D    10   8