HOWTO · R

How to Find the Index of an Element in an R Vector.

Learn how to find every matching position in an R vector with which() or the first position for each requested value with match(), including how unmatched values are handled.

On this page

Use which() when you need every position where a condition is true. Use match() when you need the first position of each requested value; it returns NA for a value with no match. R vector positions start at 1. The example below uses one vector to compare both results.

Find all matching positions with which()

which() accepts a logical vector and returns the positions of its TRUE elements. Comparing a vector with one value can produce several matches when that value occurs more than once. To search for any of several values, use %in% to create a logical membership vector, then pass it to which().

Run this example to find all occurrences of 20, then positions containing either 10 or 30:

x <- c(10, 20, 10, 30, 20)
which(x == 20)
which(x %in% c(10, 30))
[1] 2 5
[1] 1 3 4

The first result includes both positions where 20 occurs; the second lists positions whose values are either 10 or 30. These positions are one-based, as in all R vector indexing.

%in% is an operator that returns one logical result per element of its left-hand vector; it is not an argument to which(). You can also search by a condition such as x > 15: create or pass that logical expression to which() to get the positions satisfying it. Because which() reports only positions corresponding to TRUE, missing logical values do not appear in its result.

Find the first matching position for each value with match()

Use match(needles, haystack) to find the first position in haystack for each value in needles. The argument order matters: put the values you are searching for first and the vector being searched second. Repeated values in the searched vector do not produce repeated positions; each requested value gets at most one result. For a requested value that is absent, match() returns NA by default.

Using the x vector from the previous section, this expression finds the first position of 20 and 30, and reports no match for 40:

x <- c(10, 20, 10, 30, 20)
match(c(20, 30, 40), x)
[1]  2  4 NA

The results correspond in order to 20, 30, and 40: 20 first appears at position 2, 30 at position 4, and 40 is absent, so its result is NA.

Choose between the functions based on the result you need: which(x == value) can return every matching position, while match(value, x) returns only the first. For several requested values, which(x %in% values) returns positions in the searched vector, whereas match(values, x) returns one first-match position per requested value, preserving the order of values. Neither function changes the vector. If you use the positions to subset another vector, remember that an unmatched match() result is NA; check with !is.na(position) before using it when a missing match should be skipped. The returned positions refer to the searched vector, so keep that vector’s order in mind when applying them to related data.