测试向量是否包含给定元素

发布于 2024-07-29 01:49:35 字数 21 浏览 4 评论 0原文

如何检查向量是否包含给定值?

How to check if a vector contains a given value?

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

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

发布评论

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

评论(8

花开浅夏 2024-08-05 01:49:35

match()(返回第一次出现)和%in%(返回布尔值)函数都是为此设计的。

v <- c('a','b','c','e')

'b' %in% v
## returns TRUE

match('b',v)
## returns the first location of 'b', in this case: 2

Both the match() (returns the first appearance) and %in% (returns a Boolean) functions are designed for this.

v <- c('a','b','c','e')

'b' %in% v
## returns TRUE

match('b',v)
## returns the first location of 'b', in this case: 2
紫轩蝶泪 2024-08-05 01:49:35

is.element() 使代码更具可读性,并且与 %in% 相同

v <- c('a','b','c','e')

is.element('b', v)
'b' %in% v
## both return TRUE

is.element('f', v)
'f' %in% v
## both return FALSE

subv <- c('a', 'f')
subv %in% v
## returns a vector TRUE FALSE
is.element(subv, v)
## returns a vector TRUE FALSE

is.element() makes for more readable code, and is identical to %in%

v <- c('a','b','c','e')

is.element('b', v)
'b' %in% v
## both return TRUE

is.element('f', v)
'f' %in% v
## both return FALSE

subv <- c('a', 'f')
subv %in% v
## returns a vector TRUE FALSE
is.element(subv, v)
## returns a vector TRUE FALSE
南汐寒笙箫 2024-08-05 01:49:35

我将根据输出对选项进行分组。 假设所有示例都使用以下向量。

v <- c('z', 'a','b','a','e')

用于检查存在:

%in%

> 'a' %in% v
[1] TRUE

any()

> any('a'==v)
[1] TRUE

is.element()

> is.element('a', v)
[1] TRUE

用于查找第一次出现:

ma​​tch()

> match('a', v)
[1] 2

用于查找作为索引向量的所有出现:

which()

> which('a' == v)
[1] 2 4

用于查找所有出现的逻辑向量

==

> 'a' == v
[1] FALSE  TRUE FALSE  TRUE FALSE

编辑:
由于评论中提到的原因,从列表中删除 grep()grepl()

I will group the options based on output. Assume the following vector for all the examples.

v <- c('z', 'a','b','a','e')

For checking presence:

%in%

> 'a' %in% v
[1] TRUE

any()

> any('a'==v)
[1] TRUE

is.element()

> is.element('a', v)
[1] TRUE

For finding first occurance:

match()

> match('a', v)
[1] 2

For finding all occurances as vector of indices:

which()

> which('a' == v)
[1] 2 4

For finding all occurances as logical vector:

==

> 'a' == v
[1] FALSE  TRUE FALSE  TRUE FALSE

Edit:
Removing grep() and grepl() from the list for reason mentioned in comments

温柔一刀 2024-08-05 01:49:35

any() 函数使得代码可读

> w <- c(1,2,3)
> any(w==1)
[1] TRUE

> v <- c('a','b','c')
> any(v=='b')
[1] TRUE

> any(v=='f')
[1] FALSE

The any() function makes for readable code

> w <- c(1,2,3)
> any(w==1)
[1] TRUE

> v <- c('a','b','c')
> any(v=='b')
[1] TRUE

> any(v=='f')
[1] FALSE
深爱成瘾 2024-08-05 01:49:35

您可以使用 %in% 运算符:

vec <- c(1, 2, 3, 4, 5)
1 %in% vec # true
10 %in% vec # false

You can use the %in% operator:

vec <- c(1, 2, 3, 4, 5)
1 %in% vec # true
10 %in% vec # false
空城仅有旧梦在 2024-08-05 01:49:35

另外,为了查找可用作“which”的元素的位置

pop <- c(3, 4, 5, 7, 13)

which(pop==13)

以及查找不包含在目标向量中的元素,可以这样做:

pop <- c(1, 2, 4, 6, 10)

Tset <- c(2, 10, 7)   # Target set

pop[which(!(pop%in%Tset))]

Also to find the position of the element "which" can be used as

pop <- c(3, 4, 5, 7, 13)

which(pop==13)

and to find the elements which are not contained in the target vector, one may do this:

pop <- c(1, 2, 4, 6, 10)

Tset <- c(2, 10, 7)   # Target set

pop[which(!(pop%in%Tset))]
苍风燃霜 2024-08-05 01:49:35

我真的很喜欢 grep() 和 grepl() 用于此目的。

grep() 返回一个整数向量,指示匹配项的位置。

yo <- c("a", "a", "b", "b", "c", "c")

grep("b", yo)
[1] 3 4

grepl() 返回一个逻辑向量,在匹配位置处带有“TRUE”。

yo <- c("a", "a", "b", "b", "c", "c")

grepl("b", yo)
[1] FALSE FALSE  TRUE  TRUE FALSE FALSE

这些函数区分大小写。

I really like grep() and grepl() for this purpose.

grep() returns a vector of integers, which indicate where matches are.

yo <- c("a", "a", "b", "b", "c", "c")

grep("b", yo)
[1] 3 4

grepl() returns a logical vector, with "TRUE" at the location of matches.

yo <- c("a", "a", "b", "b", "c", "c")

grepl("b", yo)
[1] FALSE FALSE  TRUE  TRUE FALSE FALSE

These functions are case-sensitive.

梦在夏天 2024-08-05 01:49:35

检查向量中是否存在元素的另一种方法是使用 inops 中的 %in{}% 语法 程序包,如下所示:

library(inops)
#> 
#> Attaching package: 'inops'
#> The following object is masked from 'package:base':
#> 
#>     <<-
v <- c('a','b','c','e')
v %in{}% c("b")
#> [1] FALSE  TRUE FALSE FALSE

reprex 包 (v2.0.1)

Another option to check if a element exists in a vector is by using the %in{}% syntax from the inops package like this:

library(inops)
#> 
#> Attaching package: 'inops'
#> The following object is masked from 'package:base':
#> 
#>     <<-
v <- c('a','b','c','e')
v %in{}% c("b")
#> [1] FALSE  TRUE FALSE FALSE

Created on 2022-07-16 by the reprex package (v2.0.1)

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