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:")
print(A)
print("Test whether above vector contains 30:")
print(is.element(30, A))
print("Test whether above vector contains 46:")
print(is.element(45, A))

                                      

OUTPUT

[1] "The original vectors are:"
[1] 10 20 30 25  9 26
[1] "Test whether above vector contains 30:"
[1] TRUE
[1] "Test whether above vector contains 45:"
[1] FALSE