[R] Loop elimination question

Murray Jorgensen maj at waikato.ac.nz
Wed Nov 1 09:55:11 CET 2000

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