R:如何按行名称从 r 数据框中获取许多(~2000)行 [英] R: How to get many (~2000) rows from an r data frame by row name
问题描述
我有一个带有列标题和行名称的大型数据框,即
I have a large data frame with column headers and row names, i.e.
CountTable = read.table( Data, header=TRUE, row.names=1 )
CountTable = read.table( Data, header=TRUE, row.names=1 )
头部(计数表)
S1 S2 S3
Row1 9 8 2
Row2 268 193 282
Row3 635 631 568
Row4 0 2 0
Row5 15 8 10
Row6 416 321 350
... etc
我想从中检索基于名称的行.如果我只有几个要检索,我会使用方括号函数,例如
From which I would like to retrieve rows based on name. If I had only a few to retrieve I would use the square bracket function, e.g.
CountTable[c(Row1", Row3", Row6",]
CountTable[c("Row1", "Row3", "Row6",]
S1 S2 S3
Row1 9 8 2
Row3 635 631 568
Row6 416 321 350
但由于我的数据框有 > 20,000 行,我想从中检索 ~2000 行,这不是很实用.我最好的想法是是否有办法从另一个文件(例如,names.txt/.cvs)导入 ~2000 个名称并创建索引向量,例如
But as my data frame has >20,000 rows from which I would like to retrieve ~2000 by their name, this isn't very practical. My best thought was if there is a way of importing the ~2000 names from another file (for example, names.txt/.cvs) and creating an index vector, e.g.
[1] Row1 Row3 Row6 ... Row2000
[1] Row1 Row3 Row6 ... Row2000
这可用于指定在创建数据子集时要检索哪些行?
That could be used to specify which rows to retrieve when creating a subset of my data?
任何解决方案将不胜感激!
Any solution would be greatly appreciated!
推荐答案
如果您基于行名进行子集化,您应该按照
If you're subsetting based on rownames, you should be doing something along the lines of
CountTable[rownames(CountTable) %in% c("row1", "row2", "row3"), ]
要构造行名向量,可以使用
To construct a vector of rownames, you can use
paste0("row", 1:10)
[1] "row1" "row2" "row3" "row4" "row5" "row6" "row7" "row8" "row9"
[10] "row10"
这篇关于R:如何按行名称从 r 数据框中获取许多(~2000)行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!