The %in% operator tells you which elements are among the numers to remove:
> a <- sample (1 : 10)
> remove <- c (2, 3, 5)
> a
[1] 10 5 2 7 1 6 3 4 8 9
> a %in% remove
[1] FALSE TRUE TRUE FALSE FALSE FALSE TRUE FALSE FALSE FALSE
> a [! a %in% remove]
[1] 10 7 1 6 4 8 9
Note that this will silently remove incomparables (stuff like NA or Inf) as well (while it will keep duplicate values in a as long as they are not listed in remove).
-
If
acan contain incomparables, butremovewill not, we can usematch, telling it to return0for non-matches and incomparables (%in%is a conventient shortcut formatch):> a <- c (a, NA, Inf) > a [1] 10 5 2 7 1 6 3 4 8 9 NA Inf > match (a, remove, nomatch = 0L, incomparables = 0L) [1] 0 3 1 0 0 0 2 0 0 0 0 0 > a [match (a, remove, nomatch = 0L, incomparables = 0L) == 0L] [1] 10 7 1 6 4 8 9 NA Infincomparables = 0is not needed as incomparables will anyways not match, but I’d include it for the sake of readability.
This is, btw., whatsetdiffdoes internally (but without theuniqueto throw away duplicates inawhich are not inremove). -
If
removecontains incomparables, you’ll have to check for them individually, e.g.if (any (is.na (remove))) a <- a [! is.na (a)](This does not distinguish
NAfromNaNbut the R manual anyways warns that one should not rely on having a difference between them)For
Inf/-Infyou’ll have to check bothsignandis.finite