Just to satisfy my curiosity: > library(microbenchmark) > > a <- 1:10 > b <- 101:110 > > microbenchmark( + m1=as.vector( rbind(a,b) ), + m2=c( rbind(a,b) ), + m3=as.vector( matrix(c(a,b), nrow=2, byrow=TRUE) ), + m4={x <- integer(length(a)*2); x[c(TRUE,FALSE)] <- a; x[c(FALSE,TRUE)] <- b; x}, + m5={x <- integer(length(a)*2); x[seq.int(1, length.out=length(a), by=2)] <- a; + x[seq.int(2,length.out=length(b), by=2)] <- b; x}, + m6=c(a,b)[order( c(seq_along(a),seq_along(b)))] + ) Unit: microseconds expr min lq median uq max neval m1 2.463 3.0795 3.285 4.1055 7.390 100 m2 1.642 2.0530 2.464 2.8740 10.675 100 m3 2.874 3.6955 4.516 4.9270 31.200 100 m4 6.979 7.5950 8.211 9.4420 28.737 100 m5 8.211 9.4420 10.263 11.0850 101.400 100 m6 10.674 11.9050 12.317 13.1380 43.927 100
Of course the timings will change (and could possibly change order) with different a and b vectors. And there are other advantages/disadvantages to each method beyond the timings. But I found this interesting. On Mon, Mar 24, 2014 at 1:19 AM, David Winsemius <dwinsem...@comcast.net> wrote: > > On Mar 22, 2014, at 3:22 PM, Tham Tran wrote: > >> Dear R users, >> >> Given two vectors x and y >> a=1 2 3 >> b=4 5 6 >> >> i want to combine them into a single vector z as 1 4 2 5 3 6 >> >> Thanks for your help > > Searching Stackoverflow for [r] interleave produced this idea fron @Arun, > which I think is superior to the c(matrix-byrow) or as.vector(matrix-byrow) > strategies because it generalizes to unequal length vectors: > >> c(a,b)[ order( c(seq_along(a), seq_along(b)))] > [1] 1 4 2 5 3 6 > > -- > > David Winsemius > Alameda, CA, USA > > ______________________________________________ > 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. 538...@gmail.com ______________________________________________ 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.