[R] basic subset question of matrix

jim holtman jholtman at gmail.com
Sat Mar 31 22:00:02 CEST 2012


I think you want:

index <- row.names(myMatrix)  %in% list_to_keep[,1]

This will give a boolean expression as long as the number of rows in
myMatrix; your original only had a list as long as list_to_keep

On Sat, Mar 31, 2012 at 12:26 PM, Peter Davidsen <pkdavidsen at gmail.com> wrote:
> 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
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



-- 
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.



More information about the R-help mailing list