Create a dummy variable based on two variables x1 and x2 (dummy=x1 only if at least one adjacent x2=yes)
Advertisements Below is the code to generate the dataframe for demonstration: d<-data.frame(x1=c(rep("no",5),rep("yes",4),rep("no",2),rep("yes",3),rep("no",2),rep("yes",3)), x2=c(rep("no",6),rep("yes",1),rep("no",9),rep("yes",2),rep("no",1)), dummy=c(rep(0,5),rep(1,4),rep(0,5),rep(0,2),rep(1,3))) I have two variables x1 and x2. What I want is a dummy variable, named as ‘dummy’, based on both x1 and x2 indicators. Specifically, the dummy should equal 1 by capturing all x1=yes values conditional that at least one of… Read More Create a dummy variable based on two variables x1 and x2 (dummy=x1 only if at least one adjacent x2=yes)