# Re: [R] problem with function "rep"

From: Berwin A Turlach <berwin_at_maths.uwa.edu.au>
Date: Thu, 12 Jun 2008 23:11:18 +0800

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???
z
sum(z)
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:
http://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-doesn_0027t-R-think-these-numbers-are-equal_003f

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.

HTH. Cheers,

Berwin

• 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

R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Thu 12 Jun 2008 - 17:33:12 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 12 Jun 2008 - 18:30:40 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.