Related Articles

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

• Last Updated : 05 Apr, 2021

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``# occured``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

 “Check for x element”

 TRUE

 “Check for y element”

 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

 “Element x presence in vector”

 FALSE

 “Element y presence in vector”

 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

 “Element ‘a’ presence in vector”

 TRUE

 “Element ‘y’ presence in vector”

 FALSE

My Personal Notes arrow_drop_up