R Program to test whether a given vector contains a specified element

How to test whether a given vector contains a specified element

Here we are explaining how to write an R program to check a specified element in the vector. Here we are using a built-in function is. element() for this checking. The vector name and search element are passed to these functions directly here. The is.element() functions help to the find specified element the given list

How element check-in a vector is implemented in R Program

Below are the steps used in the R program to check the element’s existence. In this R program, we directly give the values to built-in functions. And print the function result. Here we used variable A for assigning vector values.

ALGORITHM

  • STEP 1 : Assign variable A with vector values
  • STEP 2 : First print original vector values
  • STEP 3 : Check the element by calling is.element
  • STEP 4 : print the result of the function
  • STEP 5 : If the element is not present return False else returning True

R Source Code

A= c(10, 20, 30, 25, 9, 26)
print("The original vectors are:")
## [1] "The original vectors are:"
print(A)
## [1] 10 20 30 25  9 26
print("Test whether above vector contains 30:")
## [1] "Test whether above vector contains 30:"
print(is.element(30, A))
## [1] TRUE
print("Test whether above vector contains 46:")
## [1] "Test whether above vector contains 46:"
print(is.element(45, A))
## [1] FALSE

R Source Code

B = c(17, 32, 24, 89, 97, 22)
print("The original vectors are:")
## [1] "The original vectors are:"
print(B)
## [1] 17 32 24 89 97 22
print("Test whether above vector contains 19:")
## [1] "Test whether above vector contains 19:"
print(is.element(19, B))
## [1] FALSE
print("Test whether above vector contains 89:")
## [1] "Test whether above vector contains 89:"
print(is.element(89, B))
## [1] TRUE