Open In App

Remove Multiple Values from Vector in R

Last Updated : 31 Aug, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

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
# create a vector 
a=c(1,2,"Sravan",4,5,"Bobby",4,5,6,"Ojaswi","Rohith",56.0)

# display a
print(a)

# Remove multiple values
a <- a[! a %in% c("Sravan",4,6, "Rohith")]        

print("---------")

# display a
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
# create a vector 
a=c(1,2,"Sravan",4,5,"Bobby",4,5,6,"Ojaswi","Rohith",56.0)

# display a
print(a)

# Remove multiple values
a <- a[! a %in% c("Sravan",1,2)]        

print("---------")

# display a
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" 


Next Article

Similar Reads