Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subsetting a matrix by row.names

I have a matrix with the following row.names:

"X1"   "X5"   "X33"  "X37"  "X52"  "X566"

Now I want to select only the rows which match the entries of a list, say:

include_list <- c("X1", "X5", "X33")

I imagine I'd do something like this:

data.subset <- subset(data, row.names == include_list)

However, this particular code does not seem to do the job. How can I perform subsetting in this way?

like image 365
histelheim Avatar asked Mar 26 '14 18:03

histelheim


People also ask

How do you subset row names in R?

Method 1: Subset dataframe by row namesThe rownames(df) method in R is used to set the names for rows of the data frame. A vector of the required row names is specified. The %in% operator in R is used to check for the presence of the data frame row names in the vector of required row names.

How do I select a row with specific names in R?

By using df[rows,columns] approach lets select the rows by row name from the R data frame. In order to select the rows specify the rows option. As you see above, our R DataFrame contains custom rows names r1 , r2 , r3 and so on.

How do I filter DataFrame by row names in R?

In order to filter data frame rows by row number or positions in R, we have to use the slice() function. this function takes the data frame object as the first argument and the row number you wanted to filter.


2 Answers

Set up some fake data:

m <- matrix(1:30, 6, 5)
rownames(m) <- c("X1", "X5", "X33", "X37", "X52", "X566")
m
#      [,1] [,2] [,3] [,4] [,5]
# X1      1    7   13   19   25
# X5      2    8   14   20   26
# X33     3    9   15   21   27
# X37     4   10   16   22   28
# X52     5   11   17   23   29
# X566    6   12   18   24   30

Here it's probably easiest to subset with matrix indexing ([):

include_list <- c("X1", "X5", "X33")
m[include_list, ]
#     [,1] [,2] [,3] [,4] [,5]
# X1     1    7   13   19   25
# X5     2    8   14   20   26
# X33    3    9   15   21   27

Alternative with subset() function:

subset(m, rownames(m) %in% include_list)
#      [,1] [,2] [,3] [,4] [,5]
# X1     1    7   13   19   25
# X5     2    8   14   20   26
# X33    3    9   15   21   27
like image 72
Rich Scriven Avatar answered Oct 14 '22 10:10

Rich Scriven


This also seems to work:

include_list <- head(read.csv("/Users/histelheim/include_list.csv", header = FALSE))
include_list <- c(do.call("cbind", include_list)) 
data[row.names(data) %in% include_list, ]
like image 43
histelheim Avatar answered Oct 14 '22 11:10

histelheim