 # How To Use The match() Function In R

Use the match() function in R when you need to find the first match in a vector. We will show you how.

## match() Function In R

What this function does is to find the first element in a vector that matches your desired value and return it. The syntax of match() looks like this:

match(x, table, nomatch, incomparables)

For the first examples, you only need to pay attention to the first two arguments. table is the vector where you want to find certain values while x contains values you want to find. Remember that long vectors are only supported by x, not table.

Let’s illustrate the capabilities of match() by creating a numeric vector containing 10 integers. You can use the functions runif() and round() in conjunction to quickly create a vector like this:

a <- round(runif(n=10, min=1, max=20), 0)
a


Output

  16 19 8 4 17 19 7 7 19 8

Now assume you don’t know in advance whether vector a contains the number 7 and if it does, where the first element is. You can use the match() function like this to find out:

match (7, a)


Output

 7

The command above returns a number, meaning there is indeed at least one element in a has value 7, and the only or first element is the 7th element in the vector. Note that R doesn’t use zero-indexing – the indexes start at 1 instead.

You can also see that match() ignores all other elements after the first match, even though they also contain value 7.

What if we provide match() with a number not available in the vector, such as 10? It will simply return NA, indicating that there is no such value in the vector you provide.

match(10, a)


Output

 NA

The match() function can also find multiple values in the same vector in a single command. You just need to combine all the values you want to find in a vector, and match() will return a vector of the same length containing the index position of the matches. If a value has no match, the NA value is returned.

match(c(2, 4, 6, 8), a)

 NA 4 NA 3

The example above tells match() to find the first positions of numbers 2, 4, 6, 8 in a. The returned vector is NA, 4, NA, 3. This means there is no element 2 or 6 in a, and the 4th and 3rd elements are the first elements that contain values 4 and 8, respectively.

If you want match() to return another integer value instead of NA when there is no match, use the nomatch argument. This command returns 0 if match() doesn’t find any element matching your provided value:

match(c(2, 4, 6, 8), a, nomatch = 0)

 0 4 0 3

If you only need to know a match occurs, use the logical vector %in%. The output contains TRUE and FALSE values indicating whether the original vector contains your desired values:

c(2, 4, 6, 8) %in% a

 FALSE TRUE FALSE TRUE

## Summary

The match() function in R can help you find the index positions of the first elements that have specific values. You can also use the companion operator %in% to find out whether such matches exist without knowing their positions exactly.

Posted in R