[R] Merge two vectors into one

David Winsemius dwinsemius at comcast.net
Mon Mar 24 08:19:53 CET 2014


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




More information about the R-help mailing list