[R] basic subset question of matrix

Peter Davidsen pkdavidsen at gmail.com
Sat Mar 31 18:26:04 CEST 2012


Dear list,

I would like to subset a large expression matrix based on rownames.

That is, I have a list (as a txt-file) with gene names that matches
some of the rows in my matrix.

I've loaded my matrix as well as gene list using the read.table() command.
myMatrix <- read.table("name_of_file.txt", header=T, row.names=1)
list_to_keep <- read.table("name_of_file.txt", header=T)

Thus, both my objects are data frames (however, "list_to_keep" only
have one column).

How do I remove/filter all the rows in my matrix that don't match the
ones in my list_to_keep data frame?

My initial attempt - although unsuccessful:
index <- list_to_keep[,1] %in% row.names(myMatrix)
subset.data <- myMatrix[index, ]

Alternatively, I could imagine that the subset() command would be
ideal, however I cannot write a functional script with this command.

Many thanks,
Peter



More information about the R-help mailing list