R: Generate a dummy variable based on the existence of one column' value in another column
问题 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%