Reputation: 14309
How to remove an element from a named vector by name? e.g.
v <- c(1, 2, 3)
names(v) <- c('a', 'b', 'c')
# how to remove b?
v['b'] <- NULL # doesn't work
Error in v["b"] <- NULL : replacement has length zero
Upvotes: 7
Views: 3793
Reputation: 388907
You could use
v[names(v) != "b"]
#a c
#1 3
Or with setdiff
v[setdiff(names(v), "b")]
Upvotes: 5