Remove Multiple Values from Vector in R
Last Updated :
31 Aug, 2021
In this article, we are going to discuss how to remove multiple values from the vector in R Programming Language.
We are going to remove multiple values using %in% operator
Syntax:
vector <- vector[! vector %in% c(elements)]
where,
- vector is the input vector
- elements is the values to be removed
- %in% operator checks the vector elements are present in the actual vector
Example 1: R program to remove multiple values from the vector
R
a= c (1,2, "Sravan" ,4,5, "Bobby" ,4,5,6, "Ojaswi" , "Rohith" ,56.0)
print (a)
a <- a[! a % in % c ( "Sravan" ,4,6, "Rohith" )]
print ( "---------" )
print (a)
|
Output:
[1] “1” “2” “Sravan” “4” “5” “Bobby” “4” “5”
[9] “6” “Ojaswi” “Rohith” “56”
[1] “———“
[1] “1” “2” “5” “Bobby” “5” “Ojaswi” “56”
Example 2: R program to remove multiple values from the vector
R
a= c (1,2, "Sravan" ,4,5, "Bobby" ,4,5,6, "Ojaswi" , "Rohith" ,56.0)
print (a)
a <- a[! a % in % c ( "Sravan" ,1,2)]
print ( "---------" )
print (a)
|
Output:
[1] “1” “2” “Sravan” “4” “5” “Bobby” “4” “5”
[9] “6” “Ojaswi” “Rohith” “56”
[1] “———“
[1] “4” “5” “Bobby” “4” “5” “6” “Ojaswi” “Rohith”
[9] “56”
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...