Re: [R] sorting multiple columns of a matrix

2011-09-23 Thread William Dunlap
Use do.call(func, listOfArgs) when you don't
know how many arguments will be passed to func.
E.g.,
  > x <- cbind(round(sin(1:10)), round(cos(1:10)), round(tan(1:10)))
  > x[do.call("order", split(x, col(x))), , drop=FALSE]
[,1] [,2] [,3]
   [1,]   -1   -11
   [2,]   -1   -11
   [3,]   -10   -3
   [4,]0   -10
   [5,]0   -10
   [6,]010
   [7,]10   -7
   [8,]10   -2
   [9,]111
  [10,]112

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com 

> -Original Message-
> From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On 
> Behalf Of Maxim
> Sent: Friday, September 23, 2011 9:22 AM
> To: r-help
> Subject: [R] sorting multiple columns of a matrix
> 
> Hi,
> 
> 
> I have a question about how to sort a matrix for multiple columns.
> 
> 
> dat<-sample(0:1,1000,replace=T)
> 
> matrix(dat,ncol=5,nrow=200)->x
> 
> 
> I want to order like the following:
> 
> 
> x[order(x[,1],x[,2],x[,3],x[,4],x[,5]),]->x
> 
> 
> My problem: the number of columns of the matrix to be sorted is variable, in
> any way I would like to sort for all columns from 1:ncol(x). How to achieve
> this?
> 
> 
> Best
> 
> 
> Maxim
> 
>   [[alternative HTML version deleted]]
> 
> __
> R-help@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.

__
R-help@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.


[R] sorting multiple columns of a matrix

2011-09-23 Thread Maxim
Hi,


I have a question about how to sort a matrix for multiple columns.


dat<-sample(0:1,1000,replace=T)

matrix(dat,ncol=5,nrow=200)->x


I want to order like the following:


x[order(x[,1],x[,2],x[,3],x[,4],x[,5]),]->x


My problem: the number of columns of the matrix to be sorted is variable, in
any way I would like to sort for all columns from 1:ncol(x). How to achieve
this?


Best


Maxim

[[alternative HTML version deleted]]

__
R-help@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.