New Col" YES"或"否"基于R中匹配的成功

时间:2018-03-30 20:37:10

标签: r binary

我只是想创建一个新列,上面写着" YES"或"否"基于x中与y中的数据匹配的数据。

x <- c("xx22","xx33","xx44", "xx55")
y <- c("xx22","ww66","xx55")
x <- data.frame(x)
y <- data.frame(y)

这是我希望得到的输出。我的实际数据集当然要大得多。

> x
     x match
1 xx22   YES
2 xx33    NO
3 xx44    NO
4 xx55   YES

这是迄今为止我所获得的(部分)解决方案。

x <- c("xx22","xx33","xx44", "xx55")
y <- c("YES", "YES", "YES")
x <- data.frame(x)
y <- data.frame(y)
y$match <- c("xx22","ww66","xx55")

> x$YN <- merge(x, y, all.x=TRUE, by ="x")
> x
     x YN.x YN.y
1 xx22 xx22  YES
2 xx33 xx33 <NA>
3 xx44 xx44 <NA>
4 xx55 xx55  YES

2 个答案:

答案 0 :(得分:1)

我们可以将dyplr::mutateifelse声明一起使用:

x <- c("xx22","xx33","xx44", "xx55")
y <- c("xx22","ww66","xx55")
x <- data.frame(x)
y <- data.frame(y)

x %>% mutate(Match = ifelse(x %in% y$y, "Yes", "No"))

答案 1 :(得分:1)

ifelse是矢量化的。因此base-R选项可以是:

x$match <- ifelse(x$x %in% y$y, "Yes", "No")
x
#      x match
# 1 xx22   Yes
# 2 xx33    No
# 3 xx44    No
# 4 xx55   Yes

注意:无需将y转换为data.framey可以仅用作表达式中的向量:

y <- c("xx22","ww66","xx55")
x$match <- ifelse(x$x %in% y, "Yes", "No")

数据

x <- c("xx22","xx33","xx44", "xx55")
y <- c("xx22","ww66","xx55")
x <- data.frame(x)
y <- data.frame(y)