Re: [R] Sorting rows of a matrix independent of each other
Kevski wrote: Oh, this seemed so simple (and I'm sure the answer will be, as usual, so thanks in advance for enlightening me). I need to sort each row of a matrix independent of the others. For example, apply(matrix, 1, sort) vQ __ 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] Sorting rows of a matrix independent of each other
Now I just need the resulting matrix: 2 8 9 4 6 7 1 3 5 On Mar 13, 2009, at 1:26 PM, Wacek Kusnierczyk wrote: Kevski wrote: Oh, this seemed so simple (and I'm sure the answer will be, as usual, so thanks in advance for enlightening me). I need to sort each row of a matrix independent of the others. For example, apply(matrix, 1, sort) Actually this give the transpose of what was requested, since the results from apply come back as column vectors. Try a slight refinement: outm - t(apply(test,1,sort)) outm [,1] [,2] [,3] [1,]289 [2,]467 [3,]135 -- David Winsemius __ 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] Sorting rows of a matrix independent of each other
On Fri, Mar 13, 2009 at 5:26 PM, Wacek Kusnierczyk waclaw.marcin.kusnierc...@idi.ntnu.no wrote: Oh, this seemed so simple (and I'm sure the answer will be, as usual, so thanks in advance for enlightening me). I need to sort each row of a matrix independent of the others. For example, apply(matrix, 1, sort) t(apply(test,1,sort)) Paul __ 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] Sorting rows of a matrix independent of each other
Got the answer: t(apply(test,1,sort)) I had played with the apply fn at one point, but noticed the results were not quite right. Wrapping it in t was the trick! Thanks! Now to use the nicely sorted rows in my real problem at hand... Cheers, Kev- -- View this message in context: http://www.nabble.com/Sorting-rows-of-a-matrix-independent-of-each-other-tp22498636p22501189.html Sent from the R help mailing list archive at Nabble.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] Sorting rows of a matrix independent of each other
David Winsemius wrote: Now I just need the resulting matrix: 2 8 9 4 6 7 1 3 5 On Mar 13, 2009, at 1:26 PM, Wacek Kusnierczyk wrote: Kevski wrote: Oh, this seemed so simple (and I'm sure the answer will be, as usual, so thanks in advance for enlightening me). I need to sort each row of a matrix independent of the others. For example, apply(matrix, 1, sort) Actually this give the transpose of what was requested, since the results from apply come back as column vectors. ouch!!! how ugly... vQ __ 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] Sorting rows of a matrix independent of each other
There is aaply in the plyer package that does not require a transpose: aaply(test, 1, sort) On Fri, Mar 13, 2009 at 11:14 AM, Kevski p...@kevski.com wrote: Oh, this seemed so simple (and I'm sure the answer will be, as usual, so thanks in advance for enlightening me). I need to sort each row of a matrix independent of the others. For example, test - matrix(c(8,7,1,2,6,5,9,4,3),nrow=3) test [,1] [,2] [,3] [1,] 8 2 9 [2,] 7 6 4 [3,] 1 5 3 I can get each row sorted well enough. sort(test[1,]) [1] 2 8 9 sort(test[2,]) [1] 4 6 7 sort(test[3,]) [1] 1 3 5 Now I just need the resulting matrix: 2 8 9 4 6 7 1 3 5 But when I try to turn this into something more automated, I only get the last row. for(e in 1:3) sorted - sort(test[e,]) sorted [1] 1 3 5 I've tried various incarnations of rbind around my loop, but to no avail. I've search my books and the forums, and I'm sure there is some nifty R function out there, but I can't seem to find it and/or apply it correctly. Cheers, Kev- -- View this message in context: http://www.nabble.com/Sorting-rows-of-a-matrix-independent-of-each-other-tp22498636p22498636.html Sent from the R help mailing list archive at Nabble.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. __ 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] Sorting rows of a matrix independent of each other
Gabor Grothendieck wrote: There is aaply in the plyer package that does not require a transpose: aaply(test, 1, sort) what about a yaaply? vQ __ 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.