Skip to Content

Complement of empty index vector is empty index vector

I am removing values from a vector by using - (minus sign) in front of the index vector. Like this:

scores <- scores[-indexes.to.delete]

Sometimes indexes.to.delete vector is empty, that is N/A. So the scores vector should then remain unchanged. However, I am getting empty scores vector when indexes.to.delete is empty.

Example:

x <- c(1, 2, 3);
y <- c(4, 5, 6);
indexes.to.delete <- which(y < x); # will return empty vector
y <- y[-indexes.to.delete]; # returns empty y vector, but I want y stay untouched

I could code an if statement checking whether indexes.to.delete is empty, but I am wondering if there is a simpler way?