I have a df called laws
, with a string for each law (one for each id):
laws <- data.frame(id=c(1,2,3),beginyear=c(2001,2002,2005),endyear=c(2003,2005,2006), law1=c(0,0,1), law2=c(1,0,1))
from which I want to create a middle name idyear
with a string for each id and year:
idyear <- data.frame(id=c(rep(1,6),rep(2,6),rep(3,6)), year=(rep(c(2001:2006),3)), law1=c(rep(0,16),1,1), law2=c(1,1,1,rep(0,13),1,1))
How can I efficiently write code to get idyear
df output from laws
df? Two variables of the law are indicator variables == 1 if idyear$year
is> = laws$beginyear
AND idyear$year
is <= laws$endyear
.
I start with R, but I'm ready to try anything (apply, loops, etc.) to make this work.