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
# 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
# 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"Â