U. Windl
U. Windl

Reputation: 4325

Delete a named value

I have a vector with named values like this:

> dput(v)
structure(c("in", "in", "out"), .Names = c("A", "B", "C"))

> v
    A     B     C 
 "in"  "in" "out"

I want to remove the value for a name like "B". I tried things like v["B"] <- NULL, v[-"B"], and v[!"B"], but none brought me closer to a solution. I feel there must be a trivial solution, but I just cannot find it (Chapter 6 of the introduction could benefit from adding an example).

Upvotes: 0

Views: 58

Answers (1)

akrun
akrun

Reputation: 887118

We can use names and !=

v1 <- v[names(v)!="B"]
v1
#   A     C 
# "in" "out" 

Upvotes: 1

Related Questions