Reputation: 13
I am trying to remove a list of specific values from another list but I cannot find any resources to help me do so.
list1 <- list("a","b", "c", "d", "e", "f", "g","h", "i", "j", "k")
list2 <- list("a","b","c","d")
list3 <- list1[-list2]
I would hope to get an output of the first list without a,b,c, or d. Instead I get
Error in -list2 : invalid argument to unary operator
Upvotes: 1
Views: 55
Reputation: 887901
We can use setdiff
as the list
elements have length
1
setdiff(list1, list2)
Or use %in%
and negate (!
)
list1[!list1 %in% list2]
Upvotes: 3