Suppose I have some character vector, which I'd like to subset to elements that don't match some regular expression. I might use the - operator to remove the subset that grep matches:
> vec <- letters[1:5]
> vec
[1] "a" "b" "c" "d" "e"
> vec[-grep("d", vec)]
[1] "a" "b" "c" "e"
I'm given back everything except the entries that matched "d". But if I search for a regular expression that isn't found, instead of getting everything back as I would expect, I get nothing back:
> vec[-grep("z", vec)]
character(0)
Why does this happen?
It's because
grepreturns an integer vector, and when there's no match, it returnsinteger(0).and the since the
-operator works elementwise, andinteger(0)has no elements, the negation doesn't change the integer vector:so
vec[-grep("z", vec)]evaluates tovec[-integer(0)]which in turn evaluates tovec[integer(0)], which ischaracter(0).You will get the behavior you expect with
invert = TRUE: