查找向量中多个元素的所有位置 [英] Finding All Positions for Multiple Elements in a Vector

查看:261
本文介绍了查找向量中多个元素的所有位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下向量:

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

如何找到8个或9个元素?

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

推荐答案

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

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

这篇关于查找向量中多个元素的所有位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆