[R] problem with function "rep"

Berwin A Turlach berwin at maths.uwa.edu.au
Thu Jun 12 17:11:18 CEST 2008

G'day Julien,

On Thu, 12 Jun 2008 16:48:43 +0200
Julien Hunt <julien.hunt at uclouvain.be> wrote:

> I am currently writing a program where I need to use function rep.
> The results I get are quite confusing. Given two 
> vectors A and B, I want to replicate a[1] b[1] 
> times, a[2] b[2] times and so on.
> All the entries of vector B are positive integers.
> My problem comes from the fact that if I sum up 
> all the elements of B, [...]

Others mentioned already the need for a reproducible example.  But my
guess is that the elements in B are calculated.  Recently, I was sent
the following code by a colleague of mine:

Hi Berwin,

Try this in R2.7.0

pai = c(.4,.1,.1,.4)
s = .5

p = diag(1-s, 4) + s * t(matrix(pai, 4, 4))
f = diag(pai) %*% p
z = 200*f

### bug???
length(rep(1:16, z))
length(rep(1:16, round(z)))

I tested the code and my answer was:

Interesting variation on FAQ 7.31:

Look at z-round(z) and where the negative residuals are.

My money is on you having the same problem and that using round(B)
instead of B in the rep() command will solve your problem.




=========================== Full address =============================
Berwin A Turlach                            Tel.: +65 6516 4416 (secr)
Dept of Statistics and Applied Probability        +65 6516 6650 (self)
Faculty of Science                          FAX : +65 6872 3919       
National University of Singapore     
6 Science Drive 2, Blk S16, Level 7          e-mail: statba at nus.edu.sg
Singapore 117546                    http://www.stat.nus.edu.sg/~statba

More information about the R-help mailing list