Often, we have a requirement to find out the index of the first occurrence of an element in the vector so that some operations can be performed on the same. There are many ways to do the same in R; we will go through a few of them.

Let us take a simple example where we will be creating a vector ‘* x_vector*’ with values (25,24,23,22,21,20) and then finding out the index of first occurrence of an element

**22**in it. We will also be handling a scenario where sometimes we are not able to find out any index from vector that matches our conditions in that case we will return

**“There is no match found”**In this article we will find the index of an element in vector using

## Finding the index of an element in vector using match () function

**match() **function does the value mapping in R. Let us have a look at its syntax :

match (arg_1, arg_2, nomatch, incomparables)

**Arguments :- **

**arg_1**– can be a vector or null and refers to the value we need to match**arg_2**– can be a vector or null and refers to the value we need to match with**nomatch –**is the value to be returned in case no match is found.**incomparables –**a vector of values that cannot be matched. Any value in**arg_1**matching a value in this vector is assigned the**nomatch**value.

**Example :- **

### Frequently Asked:

- R: Create empty vectors and add new items to it
- Creating Vectors in R Programming (6 ways)
- R: Create vector of zeros
- R: Set working directory

In the below code we are trying to find an element with value **22** in the **x_vector** and returning the index of its first occurrence in the same.

x_vector <- c(25,24,23,22,21) elem <- 22 result_index <- match(elem,x_vector) if (is.na(result_index)) { "There is no match found" } else { result_index }

**Output :-**

4

If we change the value of elem with ** 2** (below code) **, “There is no match found” **is printed on the console as result_index is NA. Note that **NA** in R represents a logical consonant of length 1 indicating a missing value.

x_vector <- c(25,24,23,22,21) elem<-2 result_index <- match(elem,x_vector) if (is.na(result_index)) { "There is no match found" } else { result_index }

**is.na()** function checks if the element is NA.

**Output :- **

"There is no match found"

## Finding the index of an element in vector using which() function with ‘in’

Though the usage of **which()** function is huge in R, for this article let us know that it returns the index of the element when used with **%in%** operator.

**Example :- **

which(x_vector %in% 22) will return 4 because at index position 4 element 22 is placed in x_vector.

x_vector <- c(25,24,23,22,21,20) result_index <- which(x_vector %in% 22) if (length(result_index) == 0) { "There is no match found" } else { result_index }

**Output :- **

4

If we pass 2 instead of 22 the string “There is no match found” is printed on the console as result_index returns an empty vector (vector of length 0).

x_vector <- c(25,24,23,22,21,20) result_index <- which(x_vector %in% 2) if (length(result_index) == 0) { "There is no match found" } else { result_index }

**Output:- **

"There is no match found"

## Finding the index of an element in vector using which() function and == operator

Using the which() function with == operator will check if the element equals the vector’s element value and as soon as found, the index of that element is returned.

**Example:-**

In the below code we when we compare elements of x_vector with 22, the index position 4 in x_vector is returned as 22 can be found at 4th index in the same.

x_vector <- c(25,24,23,22,21,20) result_index <- which(x_vector == 22) result_index if (length(result_index) == 0) { "There is no match found" } else { result_index }

**Output :- **

4

Let’s say we are comparing with 2 instead of 22 , then we will get the output “There is no match found” printed on console because 2 does not exist in x_vector. The result_index in this case returns us an empty vector.

x_vector <- c(25,24,23,22,21,20) result_index <- which(x_vector == 2) result_index if (length(result_index) == 0) { "There is no match found" } else { result_index }

**Output :- **

"There is no match found"

## Finding the index of an element in vector using for loop

Like in most of the programming languages, we can make the most of ‘for-loop’ in R as well, including to find the first occurrence of an index in the vector.

In the below code, for-loop iterates each element in x_vector, and a condition is checked in the if statement. If the code meets the condition within the if statement, we print the index position else, we print “There is no match found” on the console.

Finding the index of element 22 in x_vector

elem <- 22 x_vector <- c(25,24,23,22,21) val <- length(x_vector) found <- FALSE i <- 1 # initialized to 1 for (val in x_vector) { if(elem == val) { found <- TRUE cat("match found at index : ", i , "\n") break } i <- i +1 } if(!(found)) print("There is no match found")

**Output :- **

4

Finding the index of element 2 in x_vector

elem <- 2 x_vector <- c(25,24,23,22,21) val <- length(x_vector) found <- FALSE i <- 1 # initialized to 1 for (val in x_vector) { if(elem == val) { found <- TRUE cat("match found at index : ", i , "\n") break } i <- i +1 } if(!(found)) print("There is no match found")

**Output :- **

"There is no match found"