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

六眼飞鱼酱① 提交于 2019-11-28 06:15:47

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

g[!rownames(g) %in% remove, ]

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

g[-which(rownames(g) %in% remove), ]

... 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.

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 ,]

I use "setdiff" as follows:

g[setdiff(rownames(g),remove),]
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!