[R] Loop elimination question

Murray Jorgensen maj at waikato.ac.nz
Wed Nov 1 20:57:48 CET 2000


Thanks for all the responses on and off the list. I'm sure I'll understand
R/S a lot better after digesting them. A lurking friend made a point to me
that one also must consider maintainability of the code. Explicit loops
have the advantage of transparency to others than the writer of the code.

Murray Jorgensen

At 21:55 01-11-00 , Murray Jorgensen wrote:
>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.ac.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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list