8.4 C
London
Friday, December 20, 2024
HomeRDescriptive Statistics in RHow to Use match() Function in R (With Examples)

How to Use match() Function in R (With Examples)

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

The match() function in R returns the position of the first match between two objects.

This function uses the following basic syntax:

match(object1, object2)

The following examples show how to use this function in different scenarios.

Example 1: Match One Value in Vector

The following code shows how to use the match() function to find the first occurrence of a specific value in a vector:

#define value to look for in vector
value #define vector of values
vector1 #find first occurrence of 10
match(value, vector1)

[1] 4

This tells us that the value 10 first occurs in the 4th position of the vector.

Note that if there are multiple values that match, only the position of the first match will be returned.

For example, the following vector has two values equal to 10 but only the position of the first 10 is returned:

#define value to look for in vector
value #define vector of values with multiple '10' values
vector1 #find first occurrence of 10
match(value, vector1)

[1] 4

The value 10 occurs in positions 4, 5, and 6, but only position 4 is returned.

Example 2: Match Values in Two Vectors

The following code shows how to use the match() function to find the first occurrence of values in one vector in another vector:

#define vectors of values
vector1 #find first occurrence of values in vector1 within vector2
match(vector1, vector2)

[1]  3 NA NA NA NA  2

Here’s how to interpret the output:

  • The first occurrence of the value 1 in vector1 occurs in position 3 of vector2.
  • The value 2 in vector1 never occurs in vector2.
  • The value 3 in vector1 never occurs in vector2.
  • The value 4 in vector1 never occurs in vector2.
  • The value 5 in vector1 never occurs in vector2.
  • The first occurrence of the value 6 in vector1 occurs in position 2 of vector2.

Note that we can also specify a different value to use in cases where there is no match.

For example, we could return a value of 0 instead of NA:

#define vectors of values
vector1 #find first occurrence of values in vector1 within vector2
match(vector1, vector2, nomatch=0)

[1] 3 0 0 0 0 2

Additional Resources

The following tutorials explain how to use other common functions in R:

How to Use summary() Function in R
How to Use the replicate() Function in R
How to Use the gsub() Function in R
How to Use the par() Function in R

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories