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.
- Remove Specific Value From Vector
- Remove Multiple Values from Vector
- Using setdiff()
- Removing Elements by Index
- Remove Elements by Name
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 a vector, first create a vector or list containing the values you want to remove. Then use bracket notation []
along with the %in%
operator, which will identify and remove the specified values from the original vector.
# 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
- Explain Character Vector in R?
- How to Get Vector Length in R?
- Add or Append Element to Vector in R?
- How to Remove NA from Vector?
- How to Create a Vector in R?
- How to Create a DataFrame From Vectors?
- How to Create Empty Vector in R?
- Create Character Vector in R?
- How to Convert Vector to List in R?
- How to Convert List to Vector in R?
- How to Concatenate Vector in R?
- Merge Vector Elements into String in R?
- How to Subset Vector in R?
- How to Sort Vector in R?
- How to Remove Duplicates from Vector in R?
- How to Remove Rows with NA in R