[R] Ordering a matrix based on cluster no

John Kane jrkrideau at yahoo.ca
Sun Jun 26 17:58:02 CEST 2011


Combine the two matrices into one data.frame and order them
Example done using data.frames rather than matrices but just use use data.frame(x,y) to convert to a data.frame

bmat <- data.frame(matrix(1:25,5))
smat <- data.frame(aa= LETTERS[1:25],
       bb = rep(c("a","b","c", "d", "e"),5))
df1  <- data.frame(smat, bmat)
orddata  <- df1[order(df1[,2],decreasing=TRUE),]

I hope this helps.


--- On Sun, 6/26/11, Aparna Sampath <aparna.sampath26 at gmail.com> wrote:

> From: Aparna Sampath <aparna.sampath26 at gmail.com>
> Subject: [R] Ordering a matrix based on cluster no
> To: r-help at r-project.org
> Received: Sunday, June 26, 2011, 9:42 AM
> Hi All
> 
> I have a symmetric matrix of genes ( 100x100 matrix). I
> also have a matrix
> (100x2) of two columns where column 1 has the gene names
> and column 2 has
> the cluster it belongs to (they are sorted and grouped
> based on the cluster
> no).
> 
> I would like to order the rows and columns of the 100x 100
> matrix such that
> the first n genes correspond to cluster 1 and next n genes
> correspond to
> cluster 2 and so on. The order of genes is taken from the
> sorted
> matrix(100x2).
> 
> Can someone tell me how to do this in R.
> 
> I tried the grep() but I get a message saying that the
> length of pattern >1
> so only first element will be compared. But i want to check
> for each gene in
> the 100x100 matrix for its cluster number and then group
> it.
> 
> I also tried the order() but it did not help either.
> 
> Thanks for the help! :)
> 
> Aparna
> 
> -- 
> Aparna Sampath
> Master of Science (Bioinformatics)
> Nanyang Technological University
> Mob no : +65 91601854
> 
>     [[alternative HTML version deleted]]
> 
> ______________________________________________
> 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.
>



More information about the R-help mailing list