返回true/fals

发布于 2025-01-27 20:46:01 字数 413 浏览 4 评论 0原文

我正在寻找一种有效的方法来创建一个布尔向量,该矢量返回true如果一个或多个指定变量的数字,例如c(1,2, 3)在另一个向量中,例如c(4,5,6,1)

在此示例中,寻求的输出将是true,因为元素1都存在于两个向量中。

据我所知,%仅允许一次检查一个变量并使用|操作员在这种情况下效率低下为了。使用InterSect()返回逻辑(0)而不是falsesum(c(1,2,3)== c(4,5,6,1))> 1返回false,因为共同元素不在同一位置。

I'm looking for an efficient way to create a boolean vector which returns TRUE if one or more of a number of specified variables e.g. c(1,2,3) are in another vector e.g. c(4,5,6,1).

In this example the output sought would be TRUE as the element 1 is present in both vectors.

As far as I know %in% only permits checking one variable at a time and using the | operator is inefficient in this case given the number of potential variables I need to check for. Using intersect() returns logical(0) rather than FALSE, and sum(c(1,2,3) == c(4,5,6,1)) > 1 returns FALSE as the common elements are not in the same position.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

凉城凉梦凉人心 2025-02-03 20:46:02

更新: @m-所说的,如果没有匹配,它还给了True!这是避免这种情况的方法:

!all(!(v1 %in% v2))

第一个答案:
另一个否定<代码>全部:

v1 <- c(1,2,3)
v2 <- c(4,5,6,1)

!all(v1 %in% v2)
[1] TRUE

Update: As commented by @M-- in case of no matches it gives back TRUE! Here is how to avoid this:

!all(!(v1 %in% v2))

First answer:
One more with negating all:

v1 <- c(1,2,3)
v2 <- c(4,5,6,1)

!all(v1 %in% v2)
[1] TRUE
牵强ㄟ 2025-02-03 20:46:02

另一个选项:

vector1 <- c(1,2,3)
vector2 <- c(4,5,6,1)

any(Reduce(intersect, list(vector1, vector2)))

输出:

[1] TRUE

Another option:

vector1 <- c(1,2,3)
vector2 <- c(4,5,6,1)

any(Reduce(intersect, list(vector1, vector2)))

Output:

[1] TRUE
仙女 2025-02-03 20:46:01

您可以使用任何

vec <- c(1,2,3)
vec2 <- c(4,5,6,1)

any(vec %in% vec2)
#[1] TRUE

You can use any:

vec <- c(1,2,3)
vec2 <- c(4,5,6,1)

any(vec %in% vec2)
#[1] TRUE
肥爪爪 2025-02-03 20:46:01

这是使用is.Element with 的另一个选项:

vec1 <- c(1,2,3)
vec2 <- c(4,5,6,1)

any(is.element(vec1, vec2))

# [1] TRUE

或其他选项是使用MATD> MATCH与任何(尽管将会可能效率较低):

any(match(vec1, vec2), na.rm = TRUE)

Here's another option using is.element with any:

vec1 <- c(1,2,3)
vec2 <- c(4,5,6,1)

any(is.element(vec1, vec2))

# [1] TRUE

Or another option is to use match with any (though would probably be less efficient):

any(match(vec1, vec2), na.rm = TRUE)
昨迟人 2025-02-03 20:46:01

使用任何是最有效的方法,但是仅提供另一种解决方案,这里是使用sum

!!sum(c(1,2,3) %in% c(4,5,6,1))

 # [1] TRUE

或我们可以使用length

length(base::intersect(c(1,2,3), c(4,5,6,1))) != 0

Using any is the most efficient way, but just to offer another solution, here's one using sum:

!!sum(c(1,2,3) %in% c(4,5,6,1))

 # [1] TRUE

Or we can use length:

length(base::intersect(c(1,2,3), c(4,5,6,1))) != 0
人海汹涌 2025-02-03 20:46:01

还有一个
不是很好的选择:

anyDuplicated( c(unique(vec), unique(vec2)) ) > 0
# [1] TRUE

One more
not great option:

anyDuplicated( c(unique(vec), unique(vec2)) ) > 0
# [1] TRUE
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文