How to get row index number for particular name(s)?

前提是你 提交于 2019-12-21 17:50:08

问题


How can one determine the row index-numbers corresponding to particular row names? I have a vector of row names, and I would like to use these to obtain a vector of the corresponding row indices in a matrix.

I tried row() and as.integer(rownames(matrix.object)), but neither seems to work.


回答1:


In addition to which, you can look at match:

m <- matrix(1:25, ncol = 5, dimnames = list(letters[1:5], LETTERS[1:5]))
vec <- c("e", "a", "c")
match(vec, rownames(m))
# [1] 5 1 3



回答2:


Try which:

which(rownames(matrix.object) %in% c("foo", "bar"))


来源:https://stackoverflow.com/questions/20663720/how-to-get-row-index-number-for-particular-names

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