Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

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




# 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” 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!