An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20090328/4c89c7e0/attachment-0002.pl>
Selecting elements from a vector (a simple question with weird results)
7 messages · Philipp Pagel, Jorge Ivan Velez, Tal Galili +2 more
I wish to reorder a simple vector of numbers by another vector of the order (and then do the same, but with a data frame rows) I try this (which doesn't work) :
aa <- c(3, 1 ,2 ) aa[aa]
[1] 2 3 1 The same won't work if I try to order a data frame:
data.frame(matrix(c(3,1,2), 3,2))[c(3,1,2),]
X1 X2 3 2 2 1 3 3 2 1 1
Looks like everything is working as expected to me. So maybe you should explain what result you would like to get. cu Philipp
Dr. Philipp Pagel Lehrstuhl f?r Genomorientierte Bioinformatik Technische Universit?t M?nchen Wissenschaftszentrum Weihenstephan 85350 Freising, Germany http://mips.gsf.de/staff/pagel
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20090328/121e2d7b/attachment-0002.pl>
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20090328/4130dc34/attachment-0002.pl>
Tal Galili <tal.galili at gmail.com> [Sat, Mar 28, 2009 at 06:48:36PM CET]:
Hello people. I wish to reorder a simple vector of numbers by another vector of the order (and then do the same, but with a data frame rows) I try this (which doesn't work) :
aa <- c(3, 1 ,2 ) aa[aa]
[1] 2 3 1
To my mind, it does what you told it to, and therefore "works" in my book. The routine orders the numbers by placing the third element first, the first second, and the second third. Maybe aa[order(aa)] does what you mean it to do? Best wishes Johannes
Johannes H?sing There is something fascinating about science.
One gets such wholesale returns of conjecture
mailto:johannes at huesing.name from such a trifling investment of fact.
http://derwisch.wikidot.com (Mark Twain, "Life on the Mississippi")
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20090329/2d37e4e0/attachment-0002.pl>
aa[order(aa)] is the same as sort, although sort is much quicker and has lower memory requirements:
aa <- rnorm(10000000) all(aa[order(aa)] == sort(aa))
[1] TRUE
system.time(sort(aa))
user system elapsed 7.27 0.08 8.25
system.time(aa[order(aa)])
user system elapsed 29.58 0.32 31.31 (yes, I have a pitifully slow computer) HTH, Andrew.
On Mar 29, 8:01?am, Tal Galili <tal.gal... at gmail.com> wrote:
Yes Johannes - That helped, thank you. On Sat, Mar 28, 2009 at 11:50 PM, Johannes Huesing <johan... at huesing.name>wrote:
Tal Galili <tal.gal... at gmail.com> [Sat, Mar 28, 2009 at 06:48:36PM CET]:
Hello people.
I wish to reorder a simple vector of numbers by another vector of the
order
(and then do the same, but with a data frame rows)
I try this (which doesn't work) :
aa <- c(3, 1 ,2 ) aa[aa]
[1] 2 3 1
To my mind, it does what you told it to, and therefore "works" in my book. The routine orders the numbers by placing the third element first, the first second, and the second third.
Maybe aa[order(aa)] does what you mean it to do?
Best wishes
Johannes
-- Johannes H?sing ? ? ? ? ? ? ? There is something fascinating about science. ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?One gets such wholesale returns of conjecture mailto:johan... at huesing.name ?from such a trifling investment of fact. http://derwisch.wikidot.com? ? ? ? (Mark Twain, "Life on the Mississippi")
-- ---------------------------------------------- My contact information: Tal Galili Phone number: 972-50-3373767 FaceBook: Tal Galili My Blogs:http://www.r-statistics.com/http://www.talgalili.comhttp://www.biostatistics.co.il ? ? ? ? [[alternative HTML version deleted]]
______________________________________________ R-h... at r-project.org mailing listhttps://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.