我有两个数据集
包含列水果的数据集1,customer_num
包含fruit2,customer_num
列的数据集2因此,假设我使用customer_num作为joiner,将数据集1与数据集2进行左连接。现在我得到了一个包含fruit和fruit2作为列变量的数据集。
如何创建一个指示符来说明水果== fruit2然后1还是0?
答案 0 :(得分:0)
dataset1 %>%
mutate(Match=ifelse(fruit==fruit2,1,0))
这将创建一个名为Match的列,如果匹配则执行1,如果不匹配则执行0
答案 1 :(得分:0)
你可以这样做(我的例子):
# I've created example of customer_num where I presumed that this are numbers
fruit <- data.frame(customer_num = c(1, 2, 3, 4, 5, 6))
fruit2 <- data.frame(customer_num = c(1, 2, 3, 10, 11, 12))
# Vector in data frame
df <- data.frame(fruit, fruit2)
# And match values / Indicator
dat<-within(df,match <- ifelse (fruit == fruit2,1,0))
# Output
customer_num customer_num.1 customer_num
1 1 1 1
2 2 2 1
3 3 3 1
4 4 10 0
5 5 11 0
6 6 12 0