You are currently viewing R Remove From Vector with Examples

How to remove elements from vector in R? By using r base [] notation and setdiff() function is used to remove values from vector. Actually by using [] notation we can select values from vector and by negative the result you can remove the selected elements.

The following examples are covered in this article.

1. Quick Examples of Remove Elements From Vector

Following are quick examples of how to remove elements or values from vector in R.


# Quick examples
 
# Remove specific value from vector
x[ !x == 'A']

# Remove multiple values by list
x[! x %in% c('A', 'D', 'E')]

# Using setdiff
setdiff(x, c('A','D','E'))

# Remove elements by index
x[-c(1,2,5)]

# Using which
x[-which(x %in% c('D','E') )]

# Remove elements by name
x <- c(C1='A',C2='B',C3='C',C4='E',C5='G')
x[! names(x) %in% c('C1','C2')]

2. Remove Specific Value From Vector

To remove a specific value from a vector use R base bracket [] notation, If you are not aware the [] notation is used to select elements, by negating this you can remove elements. The following example removes value 'A'.


# Remove specific value from vector
x <- c('A','B','C','D','E','F','B')
x[ !x == 'A']

# Output
#[1] "B" "C" "D" "E" "F" "B"

Let’s see how we got this result


# Explanation
> x == 'A'
[1]  TRUE FALSE FALSE FALSE FALSE FALSE FALSE
> !x == 'A'
[1] FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
> x[ !x == 'A']
[1] "B" "C" "D" "E" "F" "B"

3. Remove Multiple Values from Vector

To remove multiple values from vector, define a vector or list with the values you wanted to remove and use it with R bracket notation [] and %in% operator. The %in% operator is used to check what values to remove.


# Remove Multiple Values
x <- c('A','B','C','D','E','F','B')
x[! x %in% c('A', 'D', 'E')]

# Output
#[1] "B" "C" "F" "B"

4. Using setdiff()

setdiff() is a base function to remove specified values from vector. The main difference between the above approach and using sediff() is, this removes the duplicates from the result. If you noticed, on our vector we have ‘B’ two times and by using sediff() returns only one value of ‘B’


# Using setdiff
# If x contains duplicat, this is eliminate
x <- c('A','B','C','D','E','F','B')
setdiff(x, c('A','D','E'))

# Output
#[1] "B" "C" "F"

5. Removing Elements by Index

Removing elements from vector by index can be done just by passing index position to the [] notation and using negation operator.


# Remove elements by index
x <- c('A','B','C','D','E','F','B')
x[-c(1,2,5)]

# Output
#[1] "C" "D" "F" "B"

6. Remove Elements by Name

To remove the elements from vector by name use R [] notation and names() function. This names() function returns names from the vector and check these names in a vector of names using %in% operator. The following example deletes C1 and C2 from vector.


# Remove elements by name
x <- c(C1='A',C2='B',C3='C',C4='E',C5='G')
x[! names(x) %in% c('C1','C2')]

# Output
# C3  C4  C5 
#"C" "E" "G" 

7. Conclusion

In this article you have learned how to remove elements or values from vector in R by using R base [] notation and setdiff() function. By using these, I have covered how to remove elements by name, by index, removing specific value, multiple values from vector. You can find the complete example from this article at Github R Programming Examples Project.

Related Articles

References

Naveen Nelamali

Naveen Nelamali (NNK) is a Data Engineer with 20+ years of experience in transforming data into actionable insights. Over the years, He has honed his expertise in designing, implementing, and maintaining data pipelines with frameworks like Apache Spark, PySpark, Pandas, R, Hive and Machine Learning. Naveen journey in the field of data engineering has been a continuous learning, innovation, and a strong commitment to data integrity. In this blog, he shares his experiences with the data as he come across. Follow Naveen @ LinkedIn and Medium