Reputation: 2890
Seems like an easy question but the solution is alluding me. I'm basically trying to remove matching characters from 2 variables in R
. The simple code below will hopefully illustrate what I'm trying to do.
If I have 2 variables x
, y
and they are filled with characters, like so:
x <- c("A", "B", "C", "D")
y <- "C"
What I'm trying to do is to search through x
... and any characters that match the characters in y
, I want to remove from x
. So, in this instance, the only character that matches is C
... so I want to remove C
from x
. Effectively, Im trying to do:
x[-3]
However, in practise, this method won't work for me. I was trying to achieve this by doing something like:
x <- x[,-which(names(x) %in% y)] # This won't work because x is not a data frame with cols
or
x <- subset(x, select=-y) # this throws back an error saying subset is missing
But I can't get these methods to work with characters... any suggestions?
Upvotes: 0
Views: 71
Reputation: 145755
x
doesn't have names, so use x
, not names(x)
. Also x
is a vector, not a data frame, so your extra comma x[,]
in the brackets won't work.
# fix those issues and this:
x[,-which(names(x) %in% y)]
# becomes this:
x[-which(x %in% y)]
# as phiver points out in comments, `which()` isn't needed, and we can use
x[!x %in% y]
# many people like to define a "not in" function for this
'%nin%' = Negate(%in%)
x[x %nin% y]
Upvotes: 2