Skip to content
Prev 244187 / 398506 Next

Creating binary variable depending on strings of two dataframes

On Dec 7, 2010, at 11:30 AM, Pete Pete wrote:

            
> dat3 <- merge(data1, data2[data2$y2=="E1", ], by.x="x1", by.y="y1",  
all.x=TRUE)
 > dat3$y2 <- 0 + (dat3$y2 %in% "E1")
 > dat3
     x1 x2 y2
1   13  0  1
2  232  1  1
3  342  0  0
4 3454  1  1
5 3455  1  0

(Admittedly not in the original order, but in my hands the R merge  
operation doesn't lend itself well to maintaining the original order.  
I see that Grothendieck's solution is better in this respect, a  
typical occurrence in comparison of our respective efforts with R.)