Creating a lag variable across two grouping variables

I have some data that is set up like the following

d <- data.frame(
  id = c(1,1,1,1,2,2,2,2,3,3,3,3) , 
  round = c(1,2,3,4,1,2,3,4,1,2,3,4) ,
  y = c(3,4,2,1,8,4,5,10,10,8,4,6)
)

I am trying to add a new variable to this data which contains the previous rounds Y for each ID (ie, a lag variable). My desired output is the following. So id 1 round 2 is equal to id 1 round 1 etc. It is fine for the first value to end up as NA

desired_output <- data.frame(
  id = c(1,1,1,1,2,2,2,2,3,3,3,3) , 
  round = c(1,2,3,4,1,2,3,4,1,2,3,4) , 
  y = c(3,4,2,1,8,4,5,10,10,8,4,6) , 
  prev_y = c(NA,3,4,2,NA,8,4,5,NA,10,8,4)
)

Either base R or tidyverse would be great, if anyone has any ideas.

Thanks!

It's quite easy actually. Sometimes the hard thing is knowing what functions are available.

d %>%
  group_by(id) %>%
  arrange(round) %>%
  mutate(prev_y = lag(y)) %>%
  ungroup()
1 Like

Oh perfect, that's great! Thanks so much. I didn't realise tidyverse had a built in function

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.