Reputation: 1251
I have a vector with some characters:
The structure glimpse(a)
List of 5
$ : chr [1:2] "Thai" "Restaurants"
$ : chr [1:2] "Vietnamese" "Restaurants"
$ : chr [1:3] "Restaurants" "Vegetarian" "Indian"
$ : chr [1:5] "Nightlife" "Bars" "Restaurants" "Thai" ...
$ : chr [1:4] "Asian Fusion" "Chinese" "Japanese" "Restaurants"
Here is a dput(a)
list(c("Thai", "Restaurants"), c("Vietnamese", "Restaurants"),
c("Restaurants", "Vegetarian", "Indian"), c("Nightlife",
"Bars", "Restaurants", "Thai", "Sports Bars"), c("Asian Fusion",
"Chinese", "Japanese", "Restaurants"))
I want to drop some values by exclusion. For example, the values below are the only values which should stay in the vector. All other values should be removed, with no replacement.
c("Chinese", "Japanese", "Sushi Bars", "Indian", "Thai", "Vietnamese", "Korean","Taiwanese", "Malaysian", "Mongolian", "Indonesian", "Laotian", "Myanmar", "Pan Asian", "Tempura", "Wok")
So, I tried the solution from this post: How to delete multiple values from a vector?
So I did:
rm <- c("Chinese", "Japanese", "Sushi Bars", "Indian", "Thai", "Vietnamese", "Korean","Taiwanese", "Malaysian", "Mongolian", "Indonesian", "Laotian", "Myanmar", "Pan Asian", "Tempura", "Wok")
a [! a %in% rm]
But nothing changes?!. Anny Idea how to remove those values from a
that doesn't belong to rm
Upvotes: 0
Views: 1707
Reputation: 886948
We can do a setdiff
if the elements are unique
by looping over the list
elements with lapply
lapply(a, setdiff, rm)
Or else, use %in%
and then negate
lapply(a, function(x) x[!x %in% rm])
Upvotes: 2