[R] insert number in vector
Eric Lecoutre
lecoutre at stat.ucl.ac.be
Tue Jun 18 13:59:12 CEST 2002
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
More information about the R-help
mailing list