R: How to get many (~2000) rows from an r data fra

2019-08-03 05:11发布

问题:

I have a large data frame with column headers and row names, i.e.

CountTable = read.table( Data, header=TRUE, row.names=1 )

head (CountTable)

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

        S1    S2    S3
Row1     9     8     2 
Row3   635   631   568
Row6   416   321   350

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

That could be used to specify which rows to retrieve when creating a subset of my data?

Any solution would be greatly appreciated!

回答1:

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 row subset