I would like to create a vector that is a sequence from 2 vectors. Could anyone provide suggestion on this? For example> a<-c(1,2,3) > b<-a+2 > b[1] 3 4 5 I would like to have a vector that is a sequence which starting from a and ending at b c = c(1:3,2:4,3:5) c = c(1,2,3,2,3,4,3,4,5) Thank you Best Regards, Suphajak Ngamlak Equity and Derivatives Trading Phatra Securities Public Company Limited Tel: +662-305-9179 Email: suphajak@phatrasecurities.com [[alternative HTML version deleted]]
Hi Suphajak, There are probably cleaner ways, but you can try this: a <- c(1,2,3) c <- unlist(lapply(a, function(x) {seq(from = x, to = x+2)})) c [1] 1 2 3 2 3 4 3 4 5 HTH, Josh On Mon, Jul 19, 2010 at 8:25 PM, Suphajak Ngamlak <Suphajak at phatrasecurities.com> wrote:> I would like to create a vector that is a sequence from 2 vectors. Could > anyone provide suggestion on this? > > For example > >> a<-c(1,2,3) >> b<-a+2 >> b > [1] 3 4 5 > > I would like to have a vector that is a sequence which starting from a > and ending at b > > c = c(1:3,2:4,3:5) > c = c(1,2,3,2,3,4,3,4,5) > > Thank you > > Best Regards, > > Suphajak Ngamlak > Equity and Derivatives Trading > Phatra Securities Public Company Limited > Tel: +662-305-9179 > Email: suphajak at phatrasecurities.com > > > > ? ? ? ?[[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. >-- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.com/
Hi: Josh's solution is much simpler (and more practical, no doubt) than the one below, but I wanted to experiment with creating sequences using a vector of start indices and a corresponding vector of end indices: b <- 1:3 # vector of start indices e <- 3:5 # vector of end indices The idea is to vectorize the seq() function, use mapply on it and then flatten the result to a vector. Intuitively obvious :) vseq <- Vectorize(seq) mapply(vseq, b, e) # close... [,1] [,2] [,3] [1,] 1 2 3 [2,] 2 3 4 [3,] 3 4 5 as.vector(mapply(vseq, b, e)) # ja... [1] 1 2 3 2 3 4 3 4 5 HTH, Dennis On Mon, Jul 19, 2010 at 8:25 PM, Suphajak Ngamlak < Suphajak@phatrasecurities.com> wrote:> I would like to create a vector that is a sequence from 2 vectors. Could > anyone provide suggestion on this? > > For example > > > a<-c(1,2,3) > > b<-a+2 > > b > [1] 3 4 5 > > I would like to have a vector that is a sequence which starting from a > and ending at b > > c = c(1:3,2:4,3:5) > c = c(1,2,3,2,3,4,3,4,5) > > Thank you > > Best Regards, > > Suphajak Ngamlak > Equity and Derivatives Trading > Phatra Securities Public Company Limited > Tel: +662-305-9179 > Email: suphajak@phatrasecurities.com > > > > [[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]]
enum.list <- function(x,y) { mylist <- NULL for(i in 1:length(x)) { mylist[[i]] <- x[i]:y[i] } xx <- unlist(mylist) } a <- c(1,2,3) b <- a+2 (harry <- enum.list(a,b) ) --- On Mon, 7/19/10, Suphajak Ngamlak <Suphajak at phatrasecurities.com> wrote:> From: Suphajak Ngamlak <Suphajak at phatrasecurities.com> > Subject: [R] Sequence from 2 Vectors > To: r-help at r-project.org > Received: Monday, July 19, 2010, 11:25 PM > I would like to create a vector that > is a sequence from 2 vectors. Could > anyone provide suggestion on this? > > For example > > > a<-c(1,2,3) > > b<-a+2 > > b > [1] 3 4 5 > > I would like to have a vector that is a sequence which > starting from a > and ending at b > > c = c(1:3,2:4,3:5) > c = c(1,2,3,2,3,4,3,4,5) > > Thank you > > Best Regards, > > Suphajak Ngamlak > Equity and Derivatives Trading > Phatra Securities Public Company Limited > Tel: +662-305-9179??? > Email: suphajak at phatrasecurities.com > > > > ??? [[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. >