[R] Bug in rep() function

Prof Brian Ripley ripley at stats.ox.ac.uk
Mon Sep 15 18:36:42 CEST 2014


On 15/09/2014 16:30, Samuel Knapp wrote:
> Dear all,
>
> I have discovered a bug in the standard rep() function: At certain

Not so:

 > a <- (1-0.9)*100
 > trunc(a)
[1] 9

As the help says

      Non-integer values of ‘times’ will be truncated towards zero.  If
      ‘times’ is a computed quantity it is prudent to add a small fuzz.

And as the posting guide said

Do your homework before posting:
...
     Read the online help for relevant functions (type ?functionname, 
e.g., ?prod, at the R prompt)


> values, rep() does not replicate the element by the proper number of times:
>
>  > a <- (1-0.9)*100
>  > a
> [1] 10
>  > length(rep(1,times=a))
> [1] 9
>  > length(rep(1,each=a))
> [1] 9
>
> As shown, this happens as well for the times= as for the each=
> parameter. It does not depend on the kind of element that is to be
> repeated:
>
>  > length(rep("abc",each=a))
> [1] 9
>
> I tried to narrow down the bug, but haven't really managed to find a
> pattern behind the bug. Here is a list with values for a (see above)
> that returns a false object ( after the value for a, i've collected the
> expected length and the length that is produced by r):
>
> # mistake at
> (1-0.9)*100       10           9
> (1-0.8)*100       20          19
> (1-0.8)*1000      200       199
> (1-0.9)*1000      100       99
> (1-0.9)*10            1         0
> (1-0.8)*10            2         1
> (1-0.9)*1000000000      100000000       99999999
> (2-1-0.9)*100         10      9
> (10/10-0.9)*100     10      9
>
> # the following sets for a work fine
> (1+0.1)*100
> (1-0.1)*100
> (1-0.7)*100
> (1-0.99)*1000
> (1-0.7)*10
> (1-0.90)*10
> (1-0.95)*100
> (1-0.95)*1000
> (2-0.9)*1000
> (2-1.9)*100
> (1.1-1)*100
> (10-9)*100
>
> Did I make any mistake? Or where else should I address this problem?
>
> Thanks and best regards,
> Samuel
>
> ______________________________________________
> 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.


-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Emeritus Professor of Applied Statistics, University of Oxford
1 South Parks Road, Oxford OX1 3TG, UK



More information about the R-help mailing list