[R] Sorting a matrix by a column

Dimitris Rizopoulos dimitris.rizopoulos at med.kuleuven.be
Mon Aug 4 21:02:55 CEST 2008


probably you need order(), e.g.,

data[order(data[, 2]), ]


I hope it helps.

Best,
Dimitris


John Sorkin wrote:
> R 2.6
> Windows XP
> 
> I have a 100x4 matirx
> 
> data<-matrix(nrow=100,ncol=4)
> 
> I would like to sort the entire matrix by column two, i.e. data[,2]
> I looked at the help page for sort() but can not determine how I can use it to sort a matrix on one of the matrix's columns.
> 
> Thanks,
> John 
> 
> John David Sorkin M.D., Ph.D.
> Chief, Biostatistics and Informatics
> University of Maryland School of Medicine Division of Gerontology
> Baltimore VA Medical Center
> 10 North Greene Street
> GRECC (BT/18/GR)
> Baltimore, MD 21201-1524
> (Phone) 410-605-7119
> (Fax) 410-605-7913 (Please call phone number above prior to faxing)
> 
> Confidentiality Statement:
> This email message, including any attachments, is for ...{{dropped:25}}



More information about the R-help mailing list