查找向量中多个元素的所有位置

发布于 2024-07-29 01:55:49 字数 138 浏览 5 评论 0原文

假设我有以下向量:

x <- c(8, 6, 9, 9, 7, 3, 2, 5, 5, 1, 6, 8, 5, 2, 9, 3, 5, 10, 8, 2)

如何找到哪些元素是 8 或 9?

Suppose I have the following vector:

x <- c(8, 6, 9, 9, 7, 3, 2, 5, 5, 1, 6, 8, 5, 2, 9, 3, 5, 10, 8, 2)

How can I find which elements are either 8 or 9?

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

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

发布评论

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

评论(7

我很OK 2024-08-05 01:55:49

这是一种方法。 首先,我得到 x 为 8 或 9 时的索引。然后我们可以验证在这些索引处,x 确实为 8 和 9。

> inds <- which(x %in% c(8,9))
> inds
[1]  1  3  4 12 15 19
> x[inds]
[1] 8 9 9 8 9 8

This is one way to do it. First I get the indices at which x is either 8 or 9. Then we can verify that at those indices, x is indeed 8 and 9.

> inds <- which(x %in% c(8,9))
> inds
[1]  1  3  4 12 15 19
> x[inds]
[1] 8 9 9 8 9 8
泪痕残 2024-08-05 01:55:49

您可以尝试使用 | 运算符来获得简短的条件

which(x == 8 | x == 9)

You could try the | operator for short conditions

which(x == 8 | x == 9)
分开我的手 2024-08-05 01:55:49

这种特定情况中,您还可以使用grep

# option 1
grep('[89]',x)
# option 2
grep('8|9',x)

两者都会给出:

[1]  1  3  4 12 15 19

当您还想检测多于一位数字的数字时,第二个选项是首选:

> grep('10|8',x)
[1]  1 12 18 19

但是,出于某种原因,我确实在回答开始时强调了这个具体案例正如 @DavidArenburg 提到的,这可能会导致导致意想不到的结果。 例如,使用 grep('1|8',x) 将检测 110

> grep('1|8',x)
[1]  1 10 12 18 19

为了避免这种副作用,您必须将要检测的数字包装在单词边界中:

> grep('\\b1\\b|8',x)
[1]  1 10 12 19

现在,未检测到 10

In this specific case you could also use grep:

# option 1
grep('[89]',x)
# option 2
grep('8|9',x)

which both give:

[1]  1  3  4 12 15 19

When you also want to detect number with more than one digit, the second option is preferred:

> grep('10|8',x)
[1]  1 12 18 19

However, I did put emphasis on this specific case at the start of my answer for a reason. As @DavidArenburg mentioned, this could lead to unintended results. Using for example grep('1|8',x) will detect both 1 and 10:

> grep('1|8',x)
[1]  1 10 12 18 19

In order to avoid that side-effect, you will have to wrap the numbers to be detected in word-bounderies:

> grep('\\b1\\b|8',x)
[1]  1 10 12 19

Now, the 10 isn't detected.

瀞厅☆埖开 2024-08-05 01:55:49

这是查找所有目标值位置的通用解决方案(仅适用于向量和一维数组)。

locate <- function(x, targets) {
    results <- lapply(targets, function(target) which(x == target))
    names(results) <- targets
    results
}

此函数返回一个列表,因为每个目标可能有任意数量的匹配项,包括零个。 该列表按目标的原始顺序排序(并命名)。

这是一个使用中的例子:

sequence <- c(1:10, 1:10)

locate(sequence, c(2,9))

这是查找所有目标值位置的通用解决方案(仅适用于向量和一维数组)。

locate <- function(x, targets) {
    results <- lapply(targets, function(target) which(x == target))
    names(results) <- targets
    results
}

此函数返回一个列表,因为每个目标可能有任意数量的匹配项,包括零个。 该列表按目标的原始顺序排序(并命名)。

这是一个使用中的例子:

2` [1] 2 12

这是查找所有目标值位置的通用解决方案(仅适用于向量和一维数组)。

locate <- function(x, targets) {
    results <- lapply(targets, function(target) which(x == target))
    names(results) <- targets
    results
}

此函数返回一个列表,因为每个目标可能有任意数量的匹配项,包括零个。 该列表按目标的原始顺序排序(并命名)。

这是一个使用中的例子:

9` [1] 9 19

Here is a generalized solution to find the locations of all target values (only works for vectors and 1-dimensional arrays).

locate <- function(x, targets) {
    results <- lapply(targets, function(target) which(x == target))
    names(results) <- targets
    results
}

This function returns a list because each target may have any number of matches, including zero. The list is sorted (and named) in the original order of the targets.

Here is an example in use:

sequence <- c(1:10, 1:10)

locate(sequence, c(2,9))

Here is a generalized solution to find the locations of all target values (only works for vectors and 1-dimensional arrays).

locate <- function(x, targets) {
    results <- lapply(targets, function(target) which(x == target))
    names(results) <- targets
    results
}

This function returns a list because each target may have any number of matches, including zero. The list is sorted (and named) in the original order of the targets.

Here is an example in use:

2` [1] 2 12

Here is a generalized solution to find the locations of all target values (only works for vectors and 1-dimensional arrays).

locate <- function(x, targets) {
    results <- lapply(targets, function(target) which(x == target))
    names(results) <- targets
    results
}

This function returns a list because each target may have any number of matches, including zero. The list is sorted (and named) in the original order of the targets.

Here is an example in use:

9` [1] 9 19
野却迷人 2024-08-05 01:55:49

或者,如果您不需要使用索引而只需要使用元素,您可以这样做

> x <- sample(1:10,20,replace=TRUE)
> x
 [1]  6  4  7  2  9  3  3  5  4  7  2  1  4  9  1  6 10  4  3 10
> x[8<=x & x<=9]
[1] 9 9

Alternatively, if you do not need to use the indices but just the elements you can do

> x <- sample(1:10,20,replace=TRUE)
> x
 [1]  6  4  7  2  9  3  3  5  4  7  2  1  4  9  1  6 10  4  3 10
> x[8<=x & x<=9]
[1] 9 9
不必在意 2024-08-05 01:55:49

如果您想使用循环找到答案,那么以下脚本将完成这项工作:

> req_nos<- c(8,9)
> pos<-list()
> for (i in 1:length(req_nos)){
  pos[[i]]<-which(x==req_nos[i])}

输出将如下所示:

>pos
[[1]]
[1] 1 12 19
[[2]] 
[1] 3  4 15

这里, pos[[1]] 包含 8 的位置,pos[[2]] 包含 9 的位置。如果您使用 %in% 方法并更改元素的输入顺序,即 c(9,8) 而不是 c(8,9),则两者的输出将相同。 该方法缓解了这样的问题。

If you want to find the answer using loops, then the following script will do the job:

> req_nos<- c(8,9)
> pos<-list()
> for (i in 1:length(req_nos)){
  pos[[i]]<-which(x==req_nos[i])}

The output will look like this:

>pos
[[1]]
[1] 1 12 19
[[2]] 
[1] 3  4 15

Here, pos[[1]] contains positions of 8 and pos[[2]] contains positions of 9. If you are using the %in% method and change the input order of elements, i.e, c(9,8) instead of c(8,9), the output will be the same for both of them. This method alleviates such problem.

埋情葬爱 2024-08-05 01:55:49

grepl 也许是一个有用的函数。 请注意,grepl 出现在 R 2.9.0 及更高版本中。 grepl 的便利之处在于它返回与 x 长度相同的逻辑向量。

grepl(8, x)
[1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[13] FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE

grepl(9, x)
[1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE
[13] FALSE FALSE FALSE FALSE  TRUE FALSE FALSE  TRUE

要得到答案,您可以执行以下操作

grepl(8,x) | grepl(9,x)

grepl maybe a useful function. Note that grepl appears in versions of R 2.9.0 and later. What's handy about grepl is that it returns a logical vector of the same length as x.

grepl(8, x)
[1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[13] FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE

grepl(9, x)
[1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE
[13] FALSE FALSE FALSE FALSE  TRUE FALSE FALSE  TRUE

To arrive at your answer, you could do the following

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