[R] Sorting a matrix by a column

Prof Brian Ripley ripley at stats.ox.ac.uk
Mon Aug 4 20:58:17 CEST 2008


On Mon, 4 Aug 2008, 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.

Rather, use sort.list, e.g.  data[sort.list(data[,2]), ]

There's an example for data frames on ?order.

>
> 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 th...{{dropped:6}}
>
> ______________________________________________
> 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.
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-help mailing list