Hello, I'm looking for some help with constructing lag variables on my dataset. I have data which is structured like the following
data.frame(
id = c(1,1,1,2,2,2,3,3,3),
Prestige = c(1,1,1,0,0,0,0,0,0),
Y = c(5,6,7,4,3,5,2,6,7)
)
What I am looking to do is create a lag variable which contains the Y value for the Prestige ID from the previous round. This is the desired output.
data.frame(
id = c(1,1,1,2,2,2,3,3,3),
Prestige = c(1,1,1,0,0,0,0,0,0),
Round = c(1,2,3,1,2,3,1,2,3) ,
Y = c(5,6,7,4,3,5,2,6,7) ,
Prev_Prestige = c(NA,5,6,NA,5,6,NA,5,6)
)
I have been trying to use the lag
function from tidyverse but I am unsure how to specify in mutate
that I want the Prev_Prestige column to be the same for every id and always be equal to the previous behaviour of the Prestige id. For example, is it possible to use filter
or something similar within mutate or make some use of case_when
?
Also, the solution needs to be based on the Prestige column as I will have many cases of this within my data. IE many different id values will have Prestige = 1.
Many thanks