Tal Galili
2009-Mar-28 17:48 UTC
[R] Selecting elements from a vector (a simple question with weird results)
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 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 How should I do this? p.s: sorry if the question is too simple, but I couldn't find a good explanation for this on google. p.p.s: I am running R 2.8 Thanks, Tal [[alternative HTML version deleted]]
Philipp Pagel
2009-Mar-28 18:03 UTC
[R] Selecting elements from a vector (a simple question with weird results)
> 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 1Looks 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
Tal Galili
2009-Mar-28 18:05 UTC
[R] Selecting elements from a vector (a simple question with weird results)
Thanks Patrick, Sorry to have missed that! Tal On Sat, Mar 28, 2009 at 9:01 PM, Patrick Burns <pburns@pburns.seanet.com>wrote:> I presume you are looking for: > > aa[order(aa)] > > and > > df[order(df[,1]), ] > > > Patrick Burns > patrick@burns-stat.com > +44 (0)20 8525 0696 > http://www.burns-stat.com > (home of "The R Inferno" and "A Guide for the Unwilling S User") > > Tal Galili wrote: > >> 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 >> >> 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 >> >> >> How should I do this? >> >> >> p.s: sorry if the question is too simple, but I couldn't find a good >> explanation for this on google. >> p.p.s: I am running R 2.8 >> >> >> Thanks, >> Tal >> >> [[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. >> >> >> >> >-- ---------------------------------------------- My contact information: Tal Galili Phone number: 972-50-3373767 FaceBook: Tal Galili My Blogs: http://www.r-statistics.com/ http://www.talgalili.com http://www.biostatistics.co.il [[alternative HTML version deleted]]
Jorge Ivan Velez
2009-Mar-28 18:08 UTC
[R] Selecting elements from a vector (a simple question with weird results)
Dear Tal, It works as it should. In this code: # Data aa <- c(3, 1 ,2 ) aa [1] 3 1 2 aa[aa] [1] 2 3 1 you are telling R to do the following: take the vector aa and select the elements aa (in R language that is aa[aa]). If you look carefully, the third element of aa is 2, the first is 3 and the second is 1. Perhaps I'm missing something but, to me, it works as expected. What would you like to get? HTH, Jorge On Sat, Mar 28, 2009 at 1:48 PM, Tal Galili <tal.galili@gmail.com> wrote:> 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 > > 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 > > > How should I do this? > > > p.s: sorry if the question is too simple, but I couldn't find a good > explanation for this on google. > p.p.s: I am running R 2.8 > > > Thanks, > Tal > > [[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. >[[alternative HTML version deleted]]
Johannes Huesing
2009-Mar-28 20:50 UTC
[R] Selecting elements from a vector (a simple question with weird results)
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 1To 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")
Apparently Analagous Threads
- How to use read.table with Hebrew column names ?
- long output in R
- How can I get "predict.lm" results with manual calculations ? (a floating point problem)
- "stack imbalance in ..." when loading a workspace
- Reading a file line by line - separating lines VS separating columns