Skip to content

a problem with rep() ?

3 messages · S.McClatchie, Uwe Ligges, Philippe GROSJEAN

#
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

Sam

Sam McClatchie, Research scientist (fisheries acoustics))))))))))
NIWA (National Institute of Water & Atmospheric Research Ltd)
PO Box 14 901, Kilbirnie, Wellington, New Zealand
s.mcclatchie at niwa.cri.nz 

                    /\
...>><xX(?> // \\
                 /// \\\   
                //// \\\\
               ///  <?)Xx><<
              /////  \\\\\\
        ><(((?>   
  >><(((?>   ...>><xX(?>O<?)Xx><<

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
"S.McClatchie" wrote:
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
Sam McClatchie wrote:

            
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._