[Rd] 'is.integer' (PR#13671)

Martin Maechler maechler at stat.math.ethz.ch
Thu Apr 23 11:37:37 CEST 2009


>>>>> "TP" == Tony Plate <tplate at acm.org>
>>>>>     on Wed, 22 Apr 2009 14:37:05 -0600 writes:

    TP> is.integer() is one of those functions with a name that can be confusing 
    TP> -- it looks at the underlying storage type of its argument (e.g., 
    TP> integer, floating point, character, etc.) not at the value stored in the 
    TP> argument.

    TP> So, the type of behavior you see is this:

    >> is.integer(1)
    TP> [1] FALSE
    >> is.integer(as.integer(1))
    TP> [1] TRUE
    >> is.integer(as.integer(1) * 1.0)
    TP> [1] FALSE
    >> is.integer(as.integer(NA))
    TP> [1] TRUE
    >> 

    TP> Careful reading of ?is.integer does tell you this, but I wouldn't accuse 
    TP> that help page of making such information blatantly obvious to new users 
    TP> of R.

    TP> To test whether a value is an integer value, you can so something like this:

    >> is.wholenumber <- function(x, tolerance = .Machine$double.eps^0.5) 
    TP> return(abs(x - round(x)) < tolerance)
    >> is.wholenumber(1)
    TP> [1] TRUE
    >> is.wholenumber(seq(1,5,by=0.5))
    TP> [1]  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE
    >> 

Thank you, Tony.

Somewhat contrary to our (R-core) beliefs that a reference
should be concise and correct, I have now added (an abridged
version of) the is.wholenumber() function to the *examples* 
on the  help(is.integer)  page and a note too.

Let's pretend we hope that this will stop such bug reports 
;-)

Martin Maechler


    TP> The 'tolerance' part is to allow for minor deviations that might be due 
    TP> to floating point representation issues, e.g., on my computer 1/49 * 49 
    TP> does not result in a value that is exactly equal to 1:

    >> 1/49 * 49 - 1
    TP> [1] -1.110223e-16
    >> is.wholenumber(1/49 * 49)
    TP> [1] TRUE
    >> is.wholenumber(1/49 * 49, tol=0)
    TP> [1] FALSE
    >> 

    TP> -- Tony Plate

    TP> hzambran.newsgroups at gmail.com wrote:
    >> Full_Name: Mauricio
    >> Version: 2.9.0 (2009-04-17)
    >> OS:  i486-pc-linux-gnu
    >> Submission from: (NULL) (193.205.203.3)
    >> 
    >> 
    >> This is a very simple function that seems not to be working, according to the
    >> definition given by '?is.integer'.
    >> 
    >> I checked in the Bug Tracking page at http://bugs.R-project.org/, but I didn't
    >> find any related message.
    >> 
    >> The possible problem is:
    >> 
    >> 
    >> 
    >>> is.integer(1)
    >>> 
    >> [1] FALSE
    >> 
    >> and 1 is obviously an integer value.
    >> 
    >> 
    >> I would really appreciate if you could clarify if this is really a bug or not.
    >> 
    >> Thanks in advance,
    >> 
    >> Mauricio
    >> 
    >> 
    >>> version
    >>> 
    >> _                           
    >> platform       i486-pc-linux-gnu           
    >> arch           i486                        
    >> os             linux-gnu                   
    >> system         i486, linux-gnu             
    >> status                                     
    >> major          2                           
    >> minor          9.0                         
    >> year           2009                        
    >> month          04                          
    >> day            17                          
    >> svn rev        48333                       
    >> language       R                           
    >> version.string R version 2.9.0 (2009-04-17)
    >> 
    >> ______________________________________________
    >> R-devel at r-project.org mailing list
    >> https://stat.ethz.ch/mailman/listinfo/r-devel
    >> 
    >> 

    TP> ______________________________________________
    TP> R-devel at r-project.org mailing list
    TP> https://stat.ethz.ch/mailman/listinfo/r-devel



More information about the R-devel mailing list