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
vec <- c (1,4,2,6)
x <- 2
flag <- FALSE
size = length (vec)
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
vec <- c ( "first" , "second" , "third" , "fourth" )
x <- "first"
y <- "is it present"
print ( "Check for x element" )
x % in % vec
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
vec <- c (1i,0+3i,5+6i,-7-2i)
x <- 1+3i
y <- -7-2i
print ( "Element x presence in vector" )
any (x==vec)
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
vec <- c ( 'a' , 'g' , 'h' , 'i' )
print ( "Element 'a' presence in vector" )
any ( 'a' ==vec)
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
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!