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!