Hi, The data is x <- c(4,3,5); I need to translate it to y <- c(1,2,3,4,1,2,3,1,2,3,4,5); i.e. for each number in x, we need to generate 1:x and put it in y. The program need to evaluate this type of calculation for millions of times in simulation. Is there any elegant way to make this happen without iteration? Many thanks!! Best, Haoda [[alternative HTML version deleted]]
Hi, Try: y<-paste("c","(",paste(unlist(sapply(x,seq)),collapse=","),")",sep="") ?y #[1] "c(1,2,3,4,1,2,3,1,2,3,4,5)" A.K. ----- Original Message ----- From: Haoda Fu <fuhds at yahoo.com.cn> To: "r-help at r-project.org" <r-help at r-project.org> Cc: "haodafu at yahoo.com" <haodafu at yahoo.com> Sent: Friday, December 28, 2012 12:43 PM Subject: [R] Any simple way to make this happen? Hi, The data is x <- c(4,3,5); I need to translate it to y <- c(1,2,3,4,1,2,3,1,2,3,4,5); i.e. for each number in x, we need to generate 1:x and put it in y. The program need to evaluate this type of calculation for millions of times in simulation. Is there any elegant way to make this happen without iteration? Many thanks!! Best, Haoda ??? [[alternative HTML version deleted]] ______________________________________________ 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.
> x <- c(4,3,5)> y <- c(1,2,3,4,1,2,3,1,2,3,4,5) > all.equal(y, sequence(x)) [1] TRUE Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf > Of Haoda Fu > Sent: Friday, December 28, 2012 9:43 AM > To: r-help at r-project.org > Cc: haodafu at yahoo.com > Subject: [R] Any simple way to make this happen? > > Hi, > > The data is > > x <- c(4,3,5); > I need to translate it to > y <- c(1,2,3,4,1,2,3,1,2,3,4,5); > i.e. for each number in x, we need to generate 1:x and put it in y. > > The program need to evaluate this type of calculation for millions of times in simulation. > Is there any elegant way to make this happen without iteration? > > Many thanks!! > > Best, > Haoda > [[alternative HTML version deleted]] > > ______________________________________________ > 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.
The only way that I can think of that would not use iteration would be to create some type of lookup table with every possible vector you may ever use, then lookup the results in that table. If you are happy with internal iteration then one possibility is:> x <- c(4,3,5) > unlist(lapply(x,seq_len))[1] 1 2 3 4 1 2 3 1 2 3 4 5 On Fri, Dec 28, 2012 at 10:43 AM, Haoda Fu <fuhds@yahoo.com.cn> wrote:> Hi, > > The data is > > x <- c(4,3,5); > I need to translate it to > y <- c(1,2,3,4,1,2,3,1,2,3,4,5); > i.e. for each number in x, we need to generate 1:x and put it in y. > > The program need to evaluate this type of calculation for millions of > times in simulation. Is there any elegant way to make this happen without > iteration? > > Many thanks!! > > Best, > Haoda > [[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. >-- Gregory (Greg) L. Snow Ph.D. 538280@gmail.com [[alternative HTML version deleted]]