How to use conditions to remove the elements In R Language

In this method, the user needs to use the specific conditions accordingly to remove the elements from a given vector.

Syntax:

vector[!condition]

where

condition: specifies the condition that element be checked

R




# create a vector
vector1=c(1,34,56,2,45,67,89,22,21,38)
 
# display
print(vector1)
 
# remove the elements with element greater
# than 50 or less than 20
print(vector1[!(vector1 > 50 | vector1 < 20)])


Output:

[1]  1 34 56  2 45 67 89 22 21 38

[1] 34 45 22 21 38

How to Remove Specific Elements from Vector in R?

In this article, we will discuss how to remove specific elements from vectors in R Programming Language.

Similar Reads

Remove elements using in operator

This operator will select specific elements and uses ! operator to exclude those elements....

Using conditions to remove the elements

...

Remove element from vector by index

...

Contact Us