简体   繁体   English

识别 R 中两个变量之间未共享的索引

[英]Identify index that is not shared between two variables in R

I would like to identify the indices for which there is not a match between two variables.我想确定两个变量之间不匹配的索引。 The following code identifies the matches rather than the mismatched:以下代码标识匹配而不是不匹配:

x <- c("a", "b", "c")
y <- c("a", "z", "c")
which(unique(as.character(x))%in% unique(y))

Thoughts on how to get this to identify the False indices (or in this example, 2)?关于如何使用它来识别 False 索引(或在此示例中为 2)的想法?

which(!(unique(as.character(x))%in% unique(y)))

cdeeterman 基本上是正确的,只需要确保 not (!) 适用于整个关系 unique(as.character(x))%in% unique(y)

You could also try using two equal signs where "x == y" basically says "x is exactly equal to y"您也可以尝试使用两个等号,其中“x == y”基本上表示“x 完全等于 y”

x = c("a", "b", "c")
y = c("a", "z", "c")

z = x == y
which(z == FALSE)

What about setdiff? setdiff 呢?

> which( y %in% setdiff(y,x)  )
[1] 2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM