To whom it may concern, 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, I get a certain value x(for example 10000). And if i calculate the length of the vector obtained after replication, I dont always get x(10000) but sometimes I get x sometimes I get 9999 instead of 10000. Has this problem been reported before? Do you need more information on my specific program. Thanks for your time and help, Best regards, Julien Hunt Julien Hunt, PhD student and teaching assistant, Institute of Statistics, Université Catholique de Louvain, Voie du Roman pays 20 B-1348 Louvain-La-Neuve, Belgium E-mail: Julien.Hunt@uclouvain.be Tel: +32 10 / 47 94 01 ***************************************************** [[alternative HTML version deleted]]
We need a reproducible example of this to tell you what is going on. Find a small example that exhibits the confusing behavior, and share it with the list. Julien Hunt wrote:> To whom it may concern, > > 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, I get a certain > value x(for example 10000). And if i calculate > the length of the vector obtained after > replication, I dont always get x(10000) but > sometimes I get x sometimes I get 9999 instead of 10000. > Has this problem been reported before? Do you > need more information on my specific program. > Thanks for your time and help, > Best regards, > > Julien Hunt > > Julien Hunt, > PhD student and teaching assistant, > Institute of Statistics, > Universit? Catholique de Louvain, > Voie du Roman pays 20 > B-1348 Louvain-La-Neuve, > Belgium > > E-mail: Julien.Hunt at uclouvain.be > Tel: +32 10 / 47 94 01 > ***************************************************** > > > [[alternative HTML version deleted]] > > > > ------------------------------------------------------------------------ > > ______________________________________________ > 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.
On Jun 12, 2008, at 10:48 AM, Julien Hunt wrote:> Do you > need more information on my specific program. > Thanks for your time and help,Yes, as the posting guide says, we would probably need a reproducible self-contained example. Emphasis on "reproducible" and "self-contained.> Best regards, > > Julien Hunt> ______________________________________________ > 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. >Haris Skiadas Department of Mathematics and Computer Science Hanover College
on 06/12/2008 09:48 AM Julien Hunt wrote:> To whom it may concern, > > 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, I get a certain > value x(for example 10000). And if i calculate > the length of the vector obtained after > replication, I dont always get x(10000) but > sometimes I get x sometimes I get 9999 instead of 10000. > Has this problem been reported before? Do you > need more information on my specific program. > Thanks for your time and help, > Best regards, > > Julien HuntAn example would be most helpful, but presuming that you are using something along the lines of: rep(a, each = b) I would check to be sure that: length(a) == length(b) lest you end up with the issue of recycling values. HTH, Marc Schwartz
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