Hi R, If x=c(1,3,5) y=c(2,4,6) I need a vector which is c(1,2,3,4,5,6) from x and y. How do I do it? I mean the best way.... Thanks, Shubha This e-mail may contain confidential and/or privileged i...{{dropped:13}}
x <- c(1,3,5) y <- c(2,4,6) sort(c(x,y))> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of Shubha > Vishwanath Karanth > Sent: Wednesday, July 23, 2008 8:55 AM > To: r-help at stat.math.ethz.ch > Subject: [R] Simple... but... > > Hi R, > > > > If > > x=c(1,3,5) > > y=c(2,4,6) > > > > I need a vector which is c(1,2,3,4,5,6) from x and y. > > > > How do I do it? I mean the best way.... > > > > Thanks, Shubha > > > > This e-mail may contain confidential and/or privileged > i...{{dropped:13}} > > ______________________________________________ > R-help at 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. >
OK, Let x=c(4,2,2) y=c(1,5,3) My result should be c(4,1,2,5,2,3) Thanks, Shubha -----Original Message----- From: Doran, Harold [mailto:HDoran at air.org] Sent: Wednesday, July 23, 2008 6:47 PM To: Shubha Vishwanath Karanth; r-help at stat.math.ethz.ch Subject: RE: [R] Simple... but... x <- c(1,3,5) y <- c(2,4,6) sort(c(x,y))> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of Shubha > Vishwanath Karanth > Sent: Wednesday, July 23, 2008 8:55 AM > To: r-help at stat.math.ethz.ch > Subject: [R] Simple... but... > > Hi R, > > > > If > > x=c(1,3,5) > > y=c(2,4,6) > > > > I need a vector which is c(1,2,3,4,5,6) from x and y. > > > > How do I do it? I mean the best way.... > > > > Thanks, Shubha > > > > This e-mail may contain confidential and/or privileged > i...{{dropped:13}} > > ______________________________________________ > R-help at 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. >This e-mail may contain confidential and/or privileged i...{{dropped:10}}
If I understand: sort(c(x,y)) or if you want combine the elements: as.vector(mapply(c, x, y)) On Wed, Jul 23, 2008 at 9:54 AM, Shubha Vishwanath Karanth <shubhak at ambaresearch.com> wrote:> Hi R, > > > > If > > x=c(1,3,5) > > y=c(2,4,6) > > > > I need a vector which is c(1,2,3,4,5,6) from x and y. > > > > How do I do it? I mean the best way.... > > > > Thanks, Shubha > > > > This e-mail may contain confidential and/or privileged i...{{dropped:13}} > > ______________________________________________ > R-help at 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
Guess this should be fine.... c(rbind(x,y)) Please let me know if there are some better ways... Thanks, Shubha -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Shubha Vishwanath Karanth Sent: Wednesday, July 23, 2008 6:25 PM To: r-help at stat.math.ethz.ch Subject: [R] Simple... but... Hi R, If x=c(1,3,5) y=c(2,4,6) I need a vector which is c(1,2,3,4,5,6) from x and y. How do I do it? I mean the best way.... Thanks, Shubha This e-mail may contain confidential and/or privileged i...{{dropped:13}} ______________________________________________ R-help at 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. This e-mail may contain confidential and/or privileged i...{{dropped:10}}
> x <- c(1,3,5) > y <- c(2,4,6) > xy <- sort(c(x,y))-----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Shubha Vishwanath Karanth Sent: Wednesday, July 23, 2008 8:55 AM To: r-help at stat.math.ethz.ch Subject: [R] Simple... but... Hi R, If x=c(1,3,5) y=c(2,4,6) I need a vector which is c(1,2,3,4,5,6) from x and y. How do I do it? I mean the best way.... Thanks, Shubha This e-mail may contain confidential and/or privileged i...{{dropped:13}} ______________________________________________ R-help at 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. P Please consider the environment before printing this e-mail Cleveland Clinic is ranked one of the top hospitals in America by U.S. News & World Report (2007). Visit us online at http://www.clevelandclinic.org for a complete listing of our services, staff and locations. Confidentiality Note: This message is intended for use\...{{dropped:13}}
Shubha Vishwanath Karanth wrote:> Hi R, > > > > If > > x=c(1,3,5) > > y=c(2,4,6) > > > > I need a vector which is c(1,2,3,4,5,6) from x and y. > > > > How do I do it? I mean the best way.... > > >the absolutely best way is: ?c vQ
Something like sort(c(x, y)) should do the trick. HTH, Thierry ------------------------------------------------------------------------ ---- ir. Thierry Onkelinx Instituut voor natuur- en bosonderzoek / Research Institute for Nature and Forest Cel biometrie, methodologie en kwaliteitszorg / Section biometrics, methodology and quality assurance Gaverstraat 4 9500 Geraardsbergen Belgium tel. + 32 54/436 185 Thierry.Onkelinx op inbo.be www.inbo.be To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of. ~ Sir Ronald Aylmer Fisher The plural of anecdote is not data. ~ Roger Brinner The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data. ~ John Tukey -----Oorspronkelijk bericht----- Van: r-help-bounces op r-project.org [mailto:r-help-bounces op r-project.org] Namens Shubha Vishwanath Karanth Verzonden: woensdag 23 juli 2008 14:55 Aan: r-help op stat.math.ethz.ch Onderwerp: [R] Simple... but... Hi R, If x=c(1,3,5) y=c(2,4,6) I need a vector which is c(1,2,3,4,5,6) from x and y. How do I do it? I mean the best way.... Thanks, Shubha This e-mail may contain confidential and/or privileged i...{{dropped:13}} ______________________________________________ R-help op 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.
Hi Shubha, Assuming you are after ordering by position in x and y (and not values), how about as.vector(t(cbind(x, y))) HTH, Jim Porzak Responsys, Inc. San Francisco, CA http://www.linkedin.com/in/jimporzak On Wed, Jul 23, 2008 at 5:54 AM, Shubha Vishwanath Karanth <shubhak at ambaresearch.com> wrote:> Hi R, > > > > If > > x=c(1,3,5) > > y=c(2,4,6) > > > > I need a vector which is c(1,2,3,4,5,6) from x and y. > > > > How do I do it? I mean the best way.... > > > > Thanks, Shubha > > > > This e-mail may contain confidential and/or privileged i...{{dropped:13}} > > ______________________________________________ > R-help at 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. >
check the following: x <- c(1,3,5) y <- c(2,4,6) c(t(cbind(x, y))) c(matrix(c(x, y), 2, by = TRUE)) I hope it helps. Best, Dimitris ---- Dimitris Rizopoulos Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/(0)16/336899 Fax: +32/(0)16/337015 Web: http://med.kuleuven.be/biostat/ http://perswww.kuleuven.be/dimitris_rizopoulos/ ----- Original Message ----- From: "Shubha Vishwanath Karanth" <shubhak at ambaresearch.com> To: <r-help at stat.math.ethz.ch> Sent: Wednesday, July 23, 2008 2:54 PM Subject: [R] Simple... but...> Hi R, > > > > If > > x=c(1,3,5) > > y=c(2,4,6) > > > > I need a vector which is c(1,2,3,4,5,6) from x and y. > > > > How do I do it? I mean the best way.... > > > > Thanks, Shubha > > > > This e-mail may contain confidential and/or privileged > i...{{dropped:13}} > > ______________________________________________ > R-help at 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. >Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
On 23-Jul-08 12:54:49, Shubha Vishwanath Karanth wrote:> Hi R, > If > x=c(1,3,5) > y=c(2,4,6) > > I need a vector which is c(1,2,3,4,5,6) from x and y. > How do I do it? I mean the best way.... > Thanks, ShubhaYour query is ambiguous, in that it is not clear whether you want a) The elements of the combination of x and y to be in increasing order, regardless of how they are distributed between x and y In which case a simple solution is sort(c(x,y)) b) The elements to be taken alternately from x and y, regardless of their values In which case a simple solution is as.vector(rbind(x,y)) Example: x <- c(1.1,1.2,1.3) y <- c(2.1,2.2,2.3) ## (a): sort(c(x,y)) # [1] 1.1 1.2 1.3 2.1 2.2 2.3 ## (b) as.vector(rbind(x,y)) # [1] 1.1 2.1 1.2 2.2 1.3 2.3 The key to solution (b) is that R reads a matrix down the columns, and rbind(x,y) puts x as a row above y as a row, so reading down the columns alternates between x and y. Hoping this helps, Ted. -------------------------------------------------------------------- E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk> Fax-to-email: +44 (0)870 094 0861 Date: 23-Jul-08 Time: 14:30:31 ------------------------------ XFMail ------------------------------
Z=NULL;for (i in 1:3) Z=c(Z,c(x[i],y[i])) On Wed, Jul 23, 2008 at 8:54 AM, Shubha Vishwanath Karanth <shubhak at ambaresearch.com> wrote:> Hi R, > > > > If > > x=c(1,3,5) > > y=c(2,4,6) > > > > I need a vector which is c(1,2,3,4,5,6) from x and y. > > > > How do I do it? I mean the best way.... > > > > Thanks, Shubha > > > > This e-mail may contain confidential and/or privileged i...{{dropped:13}} > > ______________________________________________ > R-help at 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. >-- Due to the recession, requests for instant gratification will be deferred until arrears in scheduled gratification have been satisfied.
This is definitely the best way: c(lapply(1:length(x), function(i, x, y) c(x[i], y[i]), x, y), recursive=TRUE) JS --- -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Shubha Vishwanath Karanth Sent: 23 July 2008 13:55 To: r-help at stat.math.ethz.ch Subject: [R] Simple... but... Hi R, If x=c(1,3,5) y=c(2,4,6) I need a vector which is c(1,2,3,4,5,6) from x and y. How do I do it? I mean the best way.... Thanks, Shubha This e-mail may contain confidential and/or privileged i...{{dropped:13}} ______________________________________________ R-help at 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.
How about this as.vector(t(cbind(x,y)))> -----Original Message----- > From: Shubha Vishwanath Karanth [mailto:shubhak at ambaresearch.com] > Sent: Wednesday, July 23, 2008 9:17 AM > To: Doran, Harold; r-help at stat.math.ethz.ch > Subject: RE: [R] Simple... but... > > OK, > > Let x=c(4,2,2) > y=c(1,5,3) > > My result should be c(4,1,2,5,2,3) > > Thanks, Shubha > > -----Original Message----- > From: Doran, Harold [mailto:HDoran at air.org] > Sent: Wednesday, July 23, 2008 6:47 PM > To: Shubha Vishwanath Karanth; r-help at stat.math.ethz.ch > Subject: RE: [R] Simple... but... > > x <- c(1,3,5) > y <- c(2,4,6) > > sort(c(x,y)) > > > -----Original Message----- > > From: r-help-bounces at r-project.org > > [mailto:r-help-bounces at r-project.org] On Behalf Of Shubha > Vishwanath > > Karanth > > Sent: Wednesday, July 23, 2008 8:55 AM > > To: r-help at stat.math.ethz.ch > > Subject: [R] Simple... but... > > > > Hi R, > > > > > > > > If > > > > x=c(1,3,5) > > > > y=c(2,4,6) > > > > > > > > I need a vector which is c(1,2,3,4,5,6) from x and y. > > > > > > > > How do I do it? I mean the best way.... > > > > > > > > Thanks, Shubha > > > > > > > > This e-mail may contain confidential and/or privileged > > i...{{dropped:13}} > > > > ______________________________________________ > > R-help at 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. > > > This e-mail may contain confidential and/or privileged > information. If you are not the intended recipient (or have > received this e-mail in error) please notify the sender > immediately and destroy this e-mail. Any unauthorized > copying, disclosure or distribution of the material in this > e-mail is strictly forbidden. Any views or opinions > presented are solely those of the author and do not > necessarily represent those of Amba Holdings Inc., and/or its > affiliates. Important additional terms relating to this > email can be obtained at http://www.ambaresearch.com/disclaimer > >
Shubha Vishwanath Karanth wrote:> Hi R, > > > > If > > x=c(1,3,5) > > y=c(2,4,6) > > > > I need a vector which is c(1,2,3,4,5,6) from x and y. > > > > How do I do it? I mean the best way.... > >... but seriously, rbind(x,y)[1:6] is one concise way to do it (x and y are bound into rows in a matrix which is the read in column-major order, as in fortran). vQ
G'day Shubha, On Wed, 23 Jul 2008 18:24:49 +0530 "Shubha Vishwanath Karanth" <shubhak at ambaresearch.com> wrote:> Hi R, > > > > If > > x=c(1,3,5) > > y=c(2,4,6) > > > > I need a vector which is c(1,2,3,4,5,6) from x and y. > > > > How do I do it? I mean the best way....R> as.vector(rbind(x,y)) [1] 1 2 3 4 5 6 Not sure whether this is the best way, that will depend on what you defined as "best", i.e. your loss function. :) HTH. Cheers, Berwin
On Wed, 23 Jul 2008, Shubha Vishwanath Karanth wrote:> If > > x=c(1,3,5) > y=c(2,4,6) > > I need a vector which is c(1,2,3,4,5,6) from x and y.I am assuming that you want to interleave the vectors, but that is not the only algorithm giving that result.> How do I do it? I mean the best way....as.vector(rbind(x,y)) c(rbind(x,y)) sort(c(x,y)) for some values of 'it' and 'best'. -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
Try c(rbind(x, y)) On Wed, Jul 23, 2008 at 8:54 AM, Shubha Vishwanath Karanth <shubhak at ambaresearch.com> wrote:> Hi R, > > > > If > > x=c(1,3,5) > > y=c(2,4,6) > > > > I need a vector which is c(1,2,3,4,5,6) from x and y. > > > > How do I do it? I mean the best way.... > > > > Thanks, Shubha > > > > This e-mail may contain confidential and/or privileged i...{{dropped:13}} > > ______________________________________________ > R-help at 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. >
I thinks this is the solution that you want! as.vector(matrix(c(x,y),byrow=T,ncol=3)) -----Mensagem original----- De: r-help-bounces em r-project.org [mailto:r-help-bounces em r-project.org]Em nome de Wacek Kusnierczyk Enviada em: quarta-feira, 23 de julho de 2008 10:25 Para: R help Assunto: Re: [R] Simple... but... Shubha Vishwanath Karanth wrote:> Hi R, > > > > If > > x=c(1,3,5) > > y=c(2,4,6) > > > > I need a vector which is c(1,2,3,4,5,6) from x and y. > > > > How do I do it? I mean the best way.... > > >the absolutely best way is: ?c vQ ______________________________________________ R-help em 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.