[R] insert number in vector

Peter Malewski malewski.peter at mh-hannover.de
Tue Jun 18 11:27:46 CEST 2002


> c(t(cbind(Vec,rep(1,length(Vec)))))
[1] 2 1 3 1 4 1

P.



On Tue, Jun 18, 2002 at 10:51:20AM +0200, Peter Dalgaard BSA wrote:
> "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
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

-- 
P.Malewski, Limmerstr.47, 30451 Hannover, 0511-2135008
At work: http://www.MH-Hannover.de 0511 532 3194 / Fax: 0511 532 3190, 
P.Malewski at tu-bs.de, peter.malewski at gmx.de, malewski.peter at mh-hannover.de.
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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