我想比较两列,如果每个单元格中的值都匹配,则创建一个新的单元格,说它是否匹配。我使用match
函数并得到奇怪的结果,或者我做错了事
我的样本数据
df <- dput(structure(list(col1 = c("day", "day", "day", "day", "day",
"day", "day", "night", "night", "day", "day", "day", "day", "day",
"day", "night", NA, "day", "night", "day"), col2 = c("day", "day",
"day", "day", "day", "day", "day", "night", "day", "day", "day",
"day", "day", "day", "day", "night", "day", "day", "day", "day"
)), row.names = c(NA, 20L), class = "data.frame"))
我要检查col1
和col2
中的值是否匹配
tmp$match <- match(tmp$col1, tmp$col2)
这就是我得到的
col1 col2 match
1 day day 1
2 day day 1
3 day day 1
4 day day 1
5 day day 1
6 day day 1
7 day day 1
8 night night 8
9 night day 8
10 day day 1
11 day day 1
12 day day 1
13 day day 1
14 day day 1
15 day day 1
16 night night 8
17 NA day 8
18 day day 1
19 night day 8
20 day day 1
如您所见,具有两个day
的任何行都返回1
但两个night
以及day
和night
的行返回值8(甚至丢失)值)
我想为match
列中具有不同值的匹配获得正确的结果