How to remove rows of a matrix by row name, rather than numerical index?

When working with indexing, you cannot use "negative" character vectors. You can convert to logical with %in%

g[!rownames(g) %in% remove, ]  # ! is logical negation

If you really wanted to use negative-indexing this could be done:

g[-which(rownames(g) %in% remove), ] #which converts to numeric, so minus sign OK

... however it has a nasty potential erroneous result that arises when there are not any rownames in the target vector. The result may be no values returned.


I use "setdiff" as follows:

g[setdiff(rownames(g),remove),]

You cannot negative index a character vector when indexing. Turn your vector remove into a boolean. I've defined a function

`%notin%` <- function(x,y) !(x %in% y) 

which can then be used as such: g[rownames(g) %notin% remove ,]