I have a data frame like this:
A B
2012,2013,2014 2011
2012,2013,2014 2012
2012,2013,2014 2013
2012,2013,2014 2014
2012,2013,2014 2015
I wanted to create a dummy variable, which indicates whether the value in column B exists in column A. 1 indicates the existence, and 0 indicates non-existant. Such that,
A B dummy
2012,2013,2014 2011 0
2012,2013,2014 2012 1
2012,2013,2014 2013 1
2012,2013,2014 2014 1
2012,2013,2014 2015 0
I have tried to use %in%
to achieve this:
df$dummy <- ifelse(df$B %in% df$A, 1, 0)
but it turned out that everything in the column of dummy
is 1
.
Same situation happened when I tried to use another method any()
:
df$dummy <- any(df$A==df$B)
everything in the column of dummy
is TRUE
.
Is there an efficient way to generate this dummy variable?
Many thanks!