I have a list:
L <- c("a","b","c","d","e")
I also have a subset of this list:
L1 <- c("b","d","e")
I am trying to create a new list that does not contain the subset list.
I have tried:
L[L!%in%L1]
L[L%in%!L1]
L[L%in%-L1]
but this does not work. I would be grateful for your help.
It should be
L[!(L %in% L1)]
Because of operator precedence (?Syntax
), you can also do
L[!L %in% L1]
Finally, you also have:
setdiff(L, L1)
You could also play with vecsets:vsetdiff
(disclaimer: I wrote this scary package). Unlike proper set theory as implemented in setdiff
, vsetdiff
will return all elements of a vector which do not appear in the second argument, thus allowing for multiple instances of a given value.
vsetdiff(L,L1)
[1] "a" "c"
vsetdiff(L1,L)
character(0)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With