Open In App

Find the First Occurrence of a Specific Element in a Vector in R

Last Updated : 25 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will discuss how to find the first occurrence of a specific element in a vector with its working example in the R Programming Language using R while loop. also explore various examples with detailed explanations to illustrate different concepts, principles, and scenarios across various fields. These examples will help us for better understand and apply these ideas in practical situations.

Syntax:

while (index <= length(vector) && vector[index] != element_to_find) {
index <- index + 1
}
if (index <= length(vector)) {
# Element found,
} else {
# Element not found
}

Example 1: Using a While Loop with Index

R




vector <- c(10,20,30,40,50)
element_to_find <- 40
 
 
index <- 1
while (index <= length(vector) && vector[index] != element_to_find) {
  index <- index + 1
}
 
if (index <= length(vector)) {
  cat(index)
} else {
  cat("Not found in the vector\n")
}


Output:

4
  • We define a vector named vector containing the values [10, 20, 30, 40, 50] and specify the element we want to find, which is element_to_find = 40.
  • We initialize a variable index to 1. This variable will be used to keep track of the current index in the vector while searching for the element.
  • The while loop is set up to check continues as long as index is less than or equal to the length of the vector and It also checks whether the element at the current index of the vector is not equal to element_to_find.
  • After the loop, we check whether the index is less than or equal to the length of the vector. If it is, it means the element was found, and we print the index where it was found.
  • In our example, since element_to_find is 40, and it’s found at the fourth position in the vector (index 4), the code will print “4”.
  • If the index is greater than the length of the vector, it means the element was not found in the vector, and it print “Not found in the vector

Example 2: Using a While Loop with which()

R




vector <- c(3, 7, 2, 5, 8, 4)
element_to_find <- 3
 
first_occurrence <- which(vector == element_to_find)[1]
 
if (!is.na(first_occurrence)) {
  cat(first_occurrence)
} else {
  cat("Not found in the vector\n")
}


Output:

1
  • If index is less than or equal to the length of the vector (index <= length(vector)), it means the element was found in the vector. You then use which(vector == element_to_find) to find all indices where the element is equal to element_to_find.
  • [1] is used to select the first occurrence of the element, and you store it in the first_occurrence variable. Finally, you use cat to print the index of the first occurrence.
  • If index is greater than the length of the vector, it means the element was not found in the vector, and you print “Not found in the vector.”

Example 3: Using a While Loop with match()

R




vector <- c(8, 8, 2, 5, 8, 4)
element_to_find <- 8
 
index <- match(element_to_find, vector)
 
if (!is.na(index)) {
  cat(index)
} else {
  cat("Not found in the vector\n")
}


Output:

1
  • We use the match function with the element_to_find and vector as its arguments. match returns the first position of element_to_find in the vector or NA if it’s not found.
  • We check if index is not NA. If it’s not NA, it means the element was found in the vector, and we print the index. If it is NA, it indicates that the element was not found in the vector.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads