Re: [R] sort (all columns of) a matrix
Untested, but a.sorted - apply(a,sort,2) ? Kajan Saied wrote: Dear R-Help Team, I have been trying to sort (all columns of) a matrix: a-matrix(a-c(1,3,4,6,6,4,6,56,4,64,86,39,4,2),length(a),2) a [,1] [,2] [1,]11 [2,]33 [3,]44 [4,]66 [5,]66 [6,]44 [7,]66 [8,] 56 56 [9,]44 [10,] 64 64 [11,] 86 86 [12,] 39 39 [13,]44 [14,]22 I would like to have the matrix sorted and the output should again be *a matrix* with nrow=length(a) and ncol=2 just like (a) but only sorted. Whatever I try (be it sort(), order()...) I receive a vector of length(a)*2... Could you please help me? Best regards, kajan [[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. -- Don McKenzie Research Ecologist Pacific Wildland Fire Sciences Lab US Forest Service Affiliate Professor College of Forest Resources and CSES Climate Impacts Group University of Washington phone: 206-732-7824 cell: 206-321-5966 d...@u.washington.edu __ 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.
Re: [R] sort (all columns of) a matrix
You need to define sorted in the context of a matrix. By each row individually, by each column individually, or by column using the whole matrix, or by row using the whole matrix? -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Kajan Saied Sent: Thursday, October 08, 2009 11:51 AM To: r-help@r-project.org Subject: [R] sort (all columns of) a matrix Dear R-Help Team, I have been trying to sort (all columns of) a matrix: a-matrix(a-c(1,3,4,6,6,4,6,56,4,64,86,39,4,2),length(a),2) a [,1] [,2] [1,]11 [2,]33 [3,]44 [4,]66 [5,]66 [6,]44 [7,]66 [8,] 56 56 [9,]44 [10,] 64 64 [11,] 86 86 [12,] 39 39 [13,]44 [14,]22 I would like to have the matrix sorted and the output should again be *a matrix* with nrow=length(a) and ncol=2 just like (a) but only sorted. Whatever I try (be it sort(), order()...) I receive a vector of length(a)*2... Could you please help me? Best regards, kajan [[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.
Re: [R] sort (all columns of) a matrix
Try this: a[order(a[,1], a[,2]),] On Thu, Oct 8, 2009 at 1:50 PM, Kajan Saied kajan.sa...@gmail.com wrote: Dear R-Help Team, I have been trying to sort (all columns of) a matrix: a-matrix(a-c(1,3,4,6,6,4,6,56,4,64,86,39,4,2),length(a),2) a [,1] [,2] [1,] 1 1 [2,] 3 3 [3,] 4 4 [4,] 6 6 [5,] 6 6 [6,] 4 4 [7,] 6 6 [8,] 56 56 [9,] 4 4 [10,] 64 64 [11,] 86 86 [12,] 39 39 [13,] 4 4 [14,] 2 2 I would like to have the matrix sorted and the output should again be *a matrix* with nrow=length(a) and ncol=2 just like (a) but only sorted. Whatever I try (be it sort(), order()...) I receive a vector of length(a)*2... Could you please help me? Best regards, kajan [[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. -- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40 S 49° 16' 22 O __ 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.
Re: [R] sort (all columns of) a matrix
I assume you want to sort the whole dataset (matrix) by ordering one column in ascending order (and order all other columns appropriately). a-matrix(a-c(1,3,4,6,6,4,6,56,4,64,86,39,4,2),length(a),2) a #sort by first column a[order(a[,1]),] #sort by second column a[order(a[,2]),] #both give the same result here because the columns are identical HTH, Daniel - cuncta stricte discussurus - -Ursprüngliche Nachricht- Von: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] Im Auftrag von Kajan Saied Gesendet: Thursday, October 08, 2009 12:51 PM An: r-help@r-project.org Betreff: [R] sort (all columns of) a matrix Dear R-Help Team, I have been trying to sort (all columns of) a matrix: a-matrix(a-c(1,3,4,6,6,4,6,56,4,64,86,39,4,2),length(a),2) a [,1] [,2] [1,]11 [2,]33 [3,]44 [4,]66 [5,]66 [6,]44 [7,]66 [8,] 56 56 [9,]44 [10,] 64 64 [11,] 86 86 [12,] 39 39 [13,]44 [14,]22 I would like to have the matrix sorted and the output should again be *a matrix* with nrow=length(a) and ncol=2 just like (a) but only sorted. Whatever I try (be it sort(), order()...) I receive a vector of length(a)*2... Could you please help me? Best regards, kajan [[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.
Re: [R] sort (all columns of) a matrix
Right. My guess is that Kajan wants: a[do.call(order,data.frame(a)),] ## this generalizes to an arbitrary number of columns ## do.call() is a very powerful and useful R feature worth learning about Yet another reason why the posting guide asks for a simple, proper, reproducible example. -- Bert Bert Gunter Genentech Nonclinical Biostatistics -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Erik Iverson Sent: Thursday, October 08, 2009 11:28 AM To: Kajan Saied; r-help@r-project.org Subject: Re: [R] sort (all columns of) a matrix You need to define sorted in the context of a matrix. By each row individually, by each column individually, or by column using the whole matrix, or by row using the whole matrix? -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Kajan Saied Sent: Thursday, October 08, 2009 11:51 AM To: r-help@r-project.org Subject: [R] sort (all columns of) a matrix Dear R-Help Team, I have been trying to sort (all columns of) a matrix: a-matrix(a-c(1,3,4,6,6,4,6,56,4,64,86,39,4,2),length(a),2) a [,1] [,2] [1,]11 [2,]33 [3,]44 [4,]66 [5,]66 [6,]44 [7,]66 [8,] 56 56 [9,]44 [10,] 64 64 [11,] 86 86 [12,] 39 39 [13,]44 [14,]22 I would like to have the matrix sorted and the output should again be *a matrix* with nrow=length(a) and ncol=2 just like (a) but only sorted. Whatever I try (be it sort(), order()...) I receive a vector of length(a)*2... Could you please help me? Best regards, kajan [[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-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.
Re: [R] sort (all columns of) a matrix
-Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Bert Gunter Sent: Thursday, October 08, 2009 11:41 AM To: 'Erik Iverson'; 'Kajan Saied'; r-help@r-project.org Subject: Re: [R] sort (all columns of) a matrix Right. My guess is that Kajan wants: a[do.call(order,data.frame(a)),] ## this generalizes to an arbitrary number of columns ## do.call() is a very powerful and useful R feature worth learning about If you are compulsive wrap the second argument to do.call() with unname(), just in case the data.frame has a column with a name matching an argument to order (currently 'decreasing' and 'na.last'). This can save you from an error like the following, where the 'decreasing' column of 'd' is taken to be the 'decreasing' argument to order, not just another column to sort: d-data.frame(weight=c(190, 121, 167, 121), decreasing=c(TRUE,TRUE,FALSE,FALSE)) d[do.call(order,d),] weight decreasing 1190 TRUE 3167 FALSE 2121 TRUE 4121 FALSE d[do.call(order,unname(d)),] weight decreasing 4121 FALSE 2121 TRUE 3167 FALSE 1190 TRUE At some point data.frame may take more pains to make sure its instances all have column names, in which case the argument needs to be wrapped with unname(as.list(...)). (Internally order() can call do.call(order,...) without using unname so it could run into the same problem.) Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com Yet another reason why the posting guide asks for a simple, proper, reproducible example. -- Bert Bert Gunter Genentech Nonclinical Biostatistics -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Erik Iverson Sent: Thursday, October 08, 2009 11:28 AM To: Kajan Saied; r-help@r-project.org Subject: Re: [R] sort (all columns of) a matrix You need to define sorted in the context of a matrix. By each row individually, by each column individually, or by column using the whole matrix, or by row using the whole matrix? -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Kajan Saied Sent: Thursday, October 08, 2009 11:51 AM To: r-help@r-project.org Subject: [R] sort (all columns of) a matrix Dear R-Help Team, I have been trying to sort (all columns of) a matrix: a-matrix(a-c(1,3,4,6,6,4,6,56,4,64,86,39,4,2),length(a),2) a [,1] [,2] [1,]11 [2,]33 [3,]44 [4,]66 [5,]66 [6,]44 [7,]66 [8,] 56 56 [9,]44 [10,] 64 64 [11,] 86 86 [12,] 39 39 [13,]44 [14,]22 I would like to have the matrix sorted and the output should again be *a matrix* with nrow=length(a) and ncol=2 just like (a) but only sorted. Whatever I try (be it sort(), order()...) I receive a vector of length(a)*2... Could you please help me? Best regards, kajan [[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-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.
Re: [R] sort (all columns of) a matrix
-Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of William Dunlap Sent: Thursday, October 08, 2009 12:01 PM To: Bert Gunter; Erik Iverson; Kajan Saied; r-help@r-project.org Subject: Re: [R] sort (all columns of) a matrix ... [recommendation to use unname() when using do.call() elided] ... (Internally order() can call do.call(order,...) without using unname so it could run into the same problem.) That was a false alarm. order() does call do.call but it would be impossible for the arguments to order to be in the list passed to it. Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com __ 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.
Re: [R] sort (all columns of) a matrix
Bill: Defensive programming seems to me to be a wise policy, so thanks for the helpful tip. -- Bert Bert Gunter Genentech Nonclinical Biostatistics -Original Message- From: William Dunlap [mailto:wdun...@tibco.com] Sent: Thursday, October 08, 2009 12:01 PM To: Bert Gunter; Erik Iverson; Kajan Saied; r-help@r-project.org Subject: RE: [R] sort (all columns of) a matrix -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Bert Gunter Sent: Thursday, October 08, 2009 11:41 AM To: 'Erik Iverson'; 'Kajan Saied'; r-help@r-project.org Subject: Re: [R] sort (all columns of) a matrix Right. My guess is that Kajan wants: a[do.call(order,data.frame(a)),] ## this generalizes to an arbitrary number of columns ## do.call() is a very powerful and useful R feature worth learning about If you are compulsive wrap the second argument to do.call() with unname(), just in case the data.frame has a column with a name matching an argument to order (currently 'decreasing' and 'na.last'). This can save you from an error like the following, where the 'decreasing' column of 'd' is taken to be the 'decreasing' argument to order, not just another column to sort: d-data.frame(weight=c(190, 121, 167, 121), decreasing=c(TRUE,TRUE,FALSE,FALSE)) d[do.call(order,d),] weight decreasing 1190 TRUE 3167 FALSE 2121 TRUE 4121 FALSE d[do.call(order,unname(d)),] weight decreasing 4121 FALSE 2121 TRUE 3167 FALSE 1190 TRUE At some point data.frame may take more pains to make sure its instances all have column names, in which case the argument needs to be wrapped with unname(as.list(...)). (Internally order() can call do.call(order,...) without using unname so it could run into the same problem.) Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com Yet another reason why the posting guide asks for a simple, proper, reproducible example. -- Bert Bert Gunter Genentech Nonclinical Biostatistics __ 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.