R: How to Find Index of Element in a Vector


Often you may want to find the index of a specific element in a vector in R.

For example, you may want to know which index position in a vector contains the value 5.

Or, you may wish to know which index positions(s) in a vector contain the value 3 or 5.

You can use the following methods to find the index of specific elements in a vector in R:

Method 1: Find Index of First Occurrence of Element in Vector

which(4 == my_vector)[[1]]

This particular example will return the first index position of the first occurrence of the value 4 in the vector named my_vector.

By using the [[1]] notation, we are able to extract only the first occurrence of the value 4 if it happens to occur multiple times in the vector.

Method 2: Find Index of All Occurrences of Element in Vector

which(4 == my_vector)

This particular example will return the index position of all occurrences of the value 4 in the vector named my_vector.

Method 3: Find Index of First Occurrence of Multiple Elements in Vector

match(c(4,10), my_vector)

This particular example will return the index position of the first occurrence of both the value 4 and the value 10 in the vector named my_vector.

Method 4: Find Index of All Occurrences of Multiple Elements in Vector

which(my_vector %in% c(4,10))

This particular example will return index position of all occurrences of the value 4 and the value 10 in the vector named my_vector.

The following examples show how to use each of these methods in practice.

Example 1: Find Index of First Occurrence of Element in Vector

We can use the following syntax to define a vector named my_vector and then find the index position of the first occurrence of the value 4 in the vector:

#create vector of values
my_vector <- c(3, 4, 4, 6, 2, 10, 15, 12, 4, 7, 10, 15)

#find index position of first occurrence of the value 4
which(4 == my_vector)[[1]]

[1] 2

This tells us that the first occurrence of the value 4 in the vector occurs in index position 2.

Example 2: Find Index of All Occurrences of Element in Vector

We can use the following syntax to define a vector named my_vector and then find the index position of all occurrences of the value 4 in the vector:

#create vector of values
my_vector <- c(3, 4, 4, 6, 2, 10, 15, 12, 4, 7, 10, 15)

#find index position of all occurrences of the value 4
which(4 == my_vector)

[1] 2 3 9

This tells us that the value 4 occurs in in index positions 2, 3 and 9 in the vector.

Example 3: Find Index of First Occurrence of Multiple Elements in Vector

We can use the following syntax to define a vector named my_vector and then find the index position of the first occurrence of the value 4 and the value 10 in the vector:

#create vector of values
my_vector <- c(3, 4, 4, 6, 2, 10, 15, 12, 4, 7, 10, 15)

#find index position of first occurrence of 4 and 10
match(c(4,10), my_vector)

[1] 2 6

This tells us that the value 4 first occurs in index position 2 and the value 10 first occurs in index position 6.

Example 4: Find Index of First Occurrence of Multiple Elements in Vector

We can use the following syntax to define a vector named my_vector and then find the index positions of all occurrences of the value 4 and the value 10 in the vector:

#create vector of values
my_vector <- c(3, 4, 4, 6, 2, 10, 15, 12, 4, 7, 10, 15)

#find index positions of all occurrences of 4 and 10
which(my_vector %in% c(4,10))

[1]  2  3  6  9 11

This tells us that the values 4 and 10 occur in index positions 2, 3, 6, 9 and 11 in the vector.

Additional Resources

The following tutorials explain how to perform other common tasks in R:

How to Sort a Matrix in R
How to Remove NA from Matrix in R
How to Convert Data Frame to Matrix in R
How to Convert a Table to a Matrix in R

Leave a Reply

Your email address will not be published. Required fields are marked *