Addition of the empty index vector - empty index vector

I am removing values ​​from the vector using the minus sign in front of the index. Like this:

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

Sometimes the indexes.to.deletevector is empty, i.e. N / A. Thus, the vector scoresmust remain unchanged. However, I get an empty scoresvector when indexes.to.deleteempty.

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 to check if it is indexes.to.deleteempty, but I wonder if there is an easier way?

+1
source share
1 answer

Use is possible;

x <- c(1, 2, 3)
y <- c(4, 5, 6)
y[!y<x]
> y[!y<x]
[1] 4 5 6

x <- c(1, 2, 3)
y <- c(4, 1, 6)
> y[!y<x]
[1] 4 6
>
+3
source

Source: https://habr.com/ru/post/1671549/


All Articles