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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
"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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Here are my solution. There should be something more "natural"... > vec_2:10 > result <- eval(parse(text=paste("c(",paste(vec,collapse=" ,1, "),")"))) > result [1] 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 1 10 Here is another way, maybe simpler: AddValue_ function(vec,addvalue=1) { tmp<- cbind(vec,addvalue) tmp<-as.vector(t(tmp)) return(tmp[1:(length(tmp)-1)]) } Eric At 10:30 18/06/2002 +0200, you 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 >_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._ >?------------------------?------------------------------------------------? | Eric Lecoutre | Statistics | | Voie du Roman Pays, 20 | Teaching assistant / Consultant | | 1348 Louvain-La-Neuve | Universit? de Louvain-la-Neuve | | Belgique | lecoutre at stat.ucl.ac.be | | (+32) (0)10 47 30 50 | http://www.stat.ucl.ac.be/ISpersonnel/lecoutre/| ?------------------------?------------------------------------------------? | We need statistical thinking, not rituals - Gigerenzer | ?-------------------------------------------------------------------------? -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
> > 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
On Tue, 18 Jun 2002, 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 > >How about: n<-length(vec1) odd<-2*(1:n)-1 vec2<-rep(1,2*n) vec2[odd]<-vec1 David Scott _________________________________________________________________ David Scott Department of Statistics Tamaki Campus The University of Auckland, PB 92019 Auckland NEW ZEALAND Phone: +64 9 373 7599 ext 6830 Fax: +64 9 373 7000 Email: d.scott at Auckland.ac.nz Webmaster, New Zealand Statistical Association: http://www.stat.auckland.ac.nz/nzsa/ -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._