[R] Loop elimination question
BXC (Bendix Carstensen)
BXC at novo.dk
Wed Nov 1 11:51:01 CET 2000
How about:
> dim
[1] 3 1 4 1 5
> N <- length(dim)
> one <- rep(c(3,4),N)
> two <- c(rep(1,N),dim)
> three <- rep(1:N,rep(2,N))+N*rep(0:1,N)
> rep(one,two[three])
[1] 3 4 4 4 3 4 3 4 4 4 4 3 4 3 4 4 4 4 4
----------------------
Bendix Carstensen
Senior Statistician
Steno Diabetes Centre
Niels Steensens Vej 2
DK-2820 Gentofte
Denmark
tel: +45 44 43 87 38
mob: +45 28 25 87 38
fax: +45 44 43 73 13
bxc at novo.dk
www.biostat.ku.dk/~bxc
----------------------
> -----Original Message-----
> From: Murray Jorgensen [mailto:maj at waikato.ac.nz]
> Sent: 1. november 2000 09:55
> To: r-help at stat.math.ethz.ch
> Subject: [R] Loop elimination question
>
>
> I want to make a vector built up from several parts. Each
> part consists of 3
> followed by a variable number of 4s. The number of 4s is
> given by a vector
> called dim, say
>
> dim <- c(3,1,4,1,5)
>
> From this I want to produce
>
> 3 4 4 4 3 4 3 4 4 4 4 3 4 3 4 4 4 4 4
>
> Now the following code seems to work
>
> type <- numeric(0)
> for (j in seq(along=dim)) {
> type <- c(type,3,rep(4,dim[j]))}
>
> and I won't really need it to work for very large dim vectors
> so you could say
> my problem is solved. I am wondering, though, if the loop can
> be eliminated.
>
>
>
> Murray Jorgensen, Department of Statistics, U of Waikato,
> Hamilton, NZ
> -----[+64-7-838-4773]---------------------------[maj at waikato.a
> c.nz]-----
> "Doubt everything or believe everything:these are two equally
> convenient
> strategies. With either we dispense with the need to think."
> http://www.stats.waikato.ac.nz/Staff/maj.html -
> Henri Poincare'
>
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
> -.-.-.-.-.-.-.-.-
> 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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._.
_._
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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