[R] insert number in vector

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Tue Jun 18 10:51:20 CEST 2002


"Juan Ramon Gonzalez" <jrgonzalez at ico.scs.es> writes:

> Hello R-users,
> 
> 
> 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)

something like

result <- rep(1, 2*length(vec1)-1)
result[seq(1, length(result), 2)] <- vec1

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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