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


February 7, 2023, Learn eTutorial
1131

How to test whether a given vector contains a specified element using R programming?

Here we explain 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 find specified elements in the given list.

 In this R program, we are using a variable 'a' for assigning vector values, Then we directly give the values to the built-in function. And print the function result.

ALGORITHM

STEP 1: Assign variable A with vector values

STEP 2: First we display the values that the user enters

STEP 3: Check the element by calling is.element

STEP 4: print the value that returns from the built-in function

STEP 5: If the element is not present return False else return 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