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

2019-04-11 19:03发布

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.

标签: r rows rowname
2条回答
家丑人穷心不美
2楼-- · 2019-04-11 19:32

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
查看更多
对你真心纯属浪费
3楼-- · 2019-04-11 19:54

Try which:

which(rownames(matrix.object) %in% c("foo", "bar"))
查看更多
登录 后发表回答