[R] insert number in vector

Ole Christensen o.christensen at lancaster.ac.uk
Tue Jun 18 10:57:27 CEST 2002


vec1<-c(2,3,4)
vec2 <- rep(1,length(vec1)*2)
vec2[seq(1,length(vec1)*2-1,by=2)]<- vec1

or

vec1<-c(2,3,4)
vec2 <- rep(1,length(vec1)*2-1)
vec2[seq(1,length(vec1)*2-1,by=2)]<- vec1

depending on whether you want a 1 in the end of the vector or not

Ole


Juan Ramon Gonzalez wrote:
> 
> 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)
> 
> Thank you,
> 
> Juan
> 
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> 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
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

-- 
Ole F. Christensen
Department of Mathematics and Statistics
Fylde College, Lancaster University 
Lancaster, LA1 4YF, England
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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