[R] a problem with rep() ?

Uwe Ligges ligges at statistik.uni-dortmund.de
Tue Jun 12 08:20:39 CEST 2001



"S.McClatchie" wrote:
> 
> Colleagues
> 
> ----------------------------------
> System info:
> Version 1.2.3  (2001-04-26) on NT
> ESS v. 5.1.18 using emacs ver. 20.4
> 
> ----------------------------------
> I am wondering if there is a problem with the function rep(). Both the
> commands using rep() below were expected to produce 8 twos, but only
> the second did.
> 
> x <- rep(2,40*(1-0.8))
> length(x)
> 
> y <- rep(2,40*0.2)
> length(y)
> 
> Have I misused the function? Should the second argument of rep() be
> limited to a single number or a vector of the same length as the first
> argument as it documents in the help? Or is this a bug?
> 
> Help will be appreciated.
> Thanks

This happens because of computational (un)exactness:

  (1-0.8) == 0.2
[1] FALSE

 options(digits=22)
 1-0.8
[1] 0.19999999999999996

 0.2
[1] 0.2

And because in this case R interprets (1-0.8) < 0.2, also
floor(40*(1*0.8)) is 7. ;-)


Uwe Ligges
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list