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

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 vectorx <- 2  # declare a flag to know if element has# occuredflag <- FALSE  # computing length of vectorsize = length(vec)  # iterating over elements of vectorfor (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 vectorx <- "first"y <-"is it present"  # check if the element x specified is present # in the vectorprint ("Check for x element")x %in% vec  # check if the element y specified is present # in the vectorprint ("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 numbersvec <- c(1i,0+3i,5+6i,-7-2i)  # declaring element to check x <- 1+3iy <- -7-2i  # check for presence of element xprint ("Element x presence in vector")any(x==vec)  # check for presence of element yprint ("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 vectorvec <- 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
Picked
R Vector-Programs
R-Vectors
R Language
R Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
How to Replace specific values in column in R DataFrame ?
How to change Row Names of DataFrame in R ?
Filter data by multiple conditions in R using Dplyr
Loops in R (for, while, repeat)
Change Color of Bars in Barchart using ggplot2 in R
How to Replace specific values in column in R DataFrame ?
How to change Row Names of DataFrame in R ?
How to Split Column Into Multiple Columns in R DataFrame?
Remove rows with NA in one column of R DataFrame
How to filter R DataFrame by values in a column?