[R] insert number in vector

J.R. Lockwood lockwood at rand.org
Tue Jun 18 15:32:43 CEST 2002


> 
> I need to create a vector inserting an 1 after each value of another vector.
> For example:
> 
> vec1<-c(2,3,4)
> 
> I need to create a vector with the values 2,1,3,1,4
> 
> 
> Does anyone know how create this vector without loops (vec1 could have 1000
> elements)

This problem came up for me in the past and I started w/ all sorts of
rep() contortions, but then I thought of a cleverish way:

interleave<-function(...){
  as.vector(rbind(...))
}

e.g.
interleave(2:4,rep(1,3))

you'll have to chop the trailing "1".

J.R. Lockwood
412-683-2300 x4941
lockwood at rand.org
http://www.rand.org/methodology/stat/members/lockwood/


-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list