- Related Questions & Answers
- How to find the index of the minimum and maximum value of a vector in R?
- How to find the index of the last occurrence of repeated values in a vector in R?
- Replace an element at a specified index of the Vector in Java
- How to find the position of NA in an R vector?
- How to find the range of a vector in R?
- How to find the index of an element in a matrix column based on some condition in R?
- How to find the number of distinct values in an R vector?
- How to find the number of positive values in an R vector?
- How to find the position of odd numbers in an R vector?
- C# program to find the index of an element in a List
- How to find the position of a non-NA value in an R vector?
- How to find the n number of largest values in an R vector?
- How to convert row index number or row index name of an R data frame to a vector?
- How to find what is the index of an element in a list in Python?
- Find the maximum element of a Vector in Java

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

There are three ways to find the index of an element in a vector.

> x <- sample(1:10) > x [1] 8 10 9 6 2 1 4 7 5 3

Using which

> which(x == 6)[[1]] [1] 4

Here we found the index of 6 in vector x.

Using match

> match(c(4,8),x) [1] 7 1

Here we found the index of 4 and 8 in vector x.

Using which with %in% > which(x %in% c(2,4)) [1] 5 7

Here we found the index of 2 and 4 in vector x.

Advertisements