x[-which(...), ] not the right approach ... Why? See what happens if which does not find a match:
x <- matrix(8, nrow = 4, ncol = 2) x[-which(x == 2 | x == 7, arr.ind = TRUE)[,1],] # [,1] [,2]
(It does not return anything, but should return the integer x .)
Instead, indexing with gates is a safer approach: you can negate the match vector without risking the odd behavior shown above. Here is an example:
x[!(rowSums(x == 2 | x == 7) > 0), , drop = FALSE]
which can also be written in shorter form:
x[!rowSums(x == 2 | x == 7), , drop = FALSE]
source share