Open In App

How to test if a vector contains the given element in R ?

Improve
Improve
Like Article
Like
Save
Share
Report

In this article, let’s discuss how to check a specific element in a vector in R Programming Language.

Method 1: Using loop

A for loop can be used to check if the element belongs to the vector. A boolean flag can be declared and initialized to False. As soon as the element is contained in the vector, the flag value is set to TRUE. The element is present at the corresponding position equivalent to loop counter value. The time operation required for checking is equivalent to O(n) where n is the size of the vector. 

Example:

R




# declaring a vector
vec <- c(1,4,2,6)
 
# elements x to check in the vector
x <- 2
 
# declare a flag to know if element has
# occurred
flag <- FALSE
 
# computing length of vector
size = length(vec)
 
# iterating over elements of vector
for (i in 1:size){
    if(x==vec[i]){
        flag <- TRUE
        cat("Element present in vector at pos",i)
    }
}
 
if(!flag)
    print("Element is not present in the vector")


Output

Element present in vector at pos 3

Method 2: %in% operator 

%in% operator can be used in R Programming Language, to check for the presence of an element inside a vector. It returns a boolean output, evaluating to TRUE if the element is present, else returns false. The first operand before the operator is the element to check and the second operator is the sequence of elements to check in. 

Example:

R




# declaring a vector
vec <- c("first","second","third","fourth")
 
# elements x and y to check in the vector
x <- "first"
y <-"is it present"
 
# check if the element x specified is present
# in the vector
print ("Check for x element")
x %in% vec
 
# check if the element y specified is present
# in the vector
print ("Check for y element")
y %in% vec


Output

[1] “Check for x element”

[1] TRUE

[1] “Check for y element”

[1] FALSE

Method 3: any() 

any() method can also be used to check for any element’s presence in the sequence of elements provided. The any function in R Programming Language determines if there are ANY of the given search terms in the given vector. It returns either a boolean TRUE or FALSE value.  any() method uses an expression == to evaluate the containment of the element in the vector, where the first operand is the element to check and right hand side operand is the vector to check the containment in. 

Example:

R




# declaring a vector comprising of
# complex numbers
vec <- c(1i,0+3i,5+6i,-7-2i)
 
# declaring element to check
x <- 1+3i
y <- -7-2i
 
# check for presence of element x
print ("Element x presence in vector")
any(x==vec)
 
# check for presence of element y
print ("Element y presence in vector")
any(y==vec)


Output

[1] “Element x presence in vector”

[1] FALSE

[1] “Element y presence in vector”

[1] TRUE

Method 4: is.element() method 

This can also be used for a similar purpose, to determine if the object specified by first argument is contained in the object pertaining to second object.

Syntax:

is.element(x,y)

Example:

R




# declaring a character vector
vec <- c('a','g','h','i')
 
# check for presence of element 'a'
print ("Element 'a' presence in vector")
any('a'==vec)
 
# check for presence of element 'y'
print ("Element 'y' presence in vector")
any('y'==vec)


Output

[1] “Element ‘a’ presence in vector”

[1] TRUE

[1] “Element ‘y’ presence in vector”

[1] FALSE



Last Updated : 08 Jun, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads