# [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 b
> times, a b 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)

HTH.

Cheers,

Berwin