[R] a problem with rep() ?
Philippe Grosjean
phgrosje at ulb.ac.be
Tue Jun 12 08:58:45 CEST 2001
Sam McClatchie wrote:
>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?
This sounds to me like a rounding problem. Recall that a computer always has
a limited precision in its calculations (except when using infinite
precision algorithms like in Mathematica) and it could sometimes produce
unexpected results. As an evidence of that,
z <- rep(2, 40*(1-0.79999999999999))
produces 8 twos, as expected. With other value, behavior of rep() is
"correct":
rep(2, 40*0.4)
and
rep(2, 40*(1-0.6))
both give 16 twos, as expected.
Best regards,
Philippe
...........]<(({°<...............<°}))><...............................
) ) ) ) ) __ __
( ( ( ( ( |__) | _
) ) ) ) ) | hilippe |__)rosjean
( ( ( ( ( Marine Biol. Lab., ULB, Belgium
) ) ) ) ) __
( ( ( ( ( |\ /| |__)
) ) ) ) ) | \/ |ariculture & |__)iostatistics
( ( ( ( (
) ) ) ) ) e-mail: phgrosje at ulb.ac.be or phgrosjean at sciviews.org
( ( ( ( ( SciViews project coordinator (http://www.sciviews.org)
) ) ) ) ) tel: 00-32-2-650.29.70 (lab), 00-32-2-673.31.33 (home)
( ( ( ( (
) ) ) ) ) "I'm 100% confident that p is between 0 and 1"
( ( ( ( ( L. Gonick & W. Smith (1993)
) ) ) ) )
.......................................................................
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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