[R] Bug in "is" ?

Keith Jewell k.jewell at campden.co.uk
Wed Sep 24 18:37:12 CEST 2008


Have you tried is.integer(7.0) in S-Plus? (I have)
Do you think 7.0 is integer?

IMHO in R there is nothing to be fixed (in this regard) except your 
understanding.

This is a computer language, not English; intuition isn't reliable, so we 
have help pages.
is.integer(x) is not intended to indicate whether the value of x is a whole 
number, it indicates whether x has class "integer".
All objects of class "integer" have whole number values, but not all objects 
with whole number values have class "integer".
If you want to know whether a value is a whole number you could try (but 
there may be a better way, and beware of computer precision)
x == as.integer(x)

If you want a value to be stored in an object of class "integer" you'd 
better say so (using as.integer or L or ...), else how is R to know what you 
want? As Martin has pointed out, the system could "guess" based on the 
presence or absence of a decimal point; I share his opinion that this would 
be a "bad thing".

Nuff said.

Keith J

<ctu at bigred.unl.edu> wrote in message 
news:20080924110837.bwdaetj1xcs8c0k4 at wm-imp-1.unl.edu...
> Thank you for all of you. Intuitively, 7 is an integer for people who 
> live in this planet. It is just very difficult for me to believe that  R 
> does not think 7 is an integer but 7L is.
>> is.integer(7)  # R 2.7.2
> [1] FALSE
> Thus, based on Martin's comments, I try it again on the S-PLUS 8.0 and  it 
> shows
>> is.integer(7)   # S-PLUS 8.0
> [1] T
>
> Hopefully, someday and someone will fix it therefore, R users don't  need 
> to use as.integer(7) to tell R that 7 is an integer.
>
> Thanks again
> Chunhao
>
>
> Quoting Martin Maechler <maechler at stat.math.ethz.ch>:
>
>>>>>>> "KJ" == Keith Jewell <k.jewell at campden.co.uk>
>>>>>>>     on Wed, 24 Sep 2008 09:46:08 +0100 writes:
>>
>>     KJ> "7" is an integer, but it's also a real.
>>     KJ> In R '?is'  and '?is.integer' are clear that you're testing   the 
>> class(es) of
>>     KJ> objects, not their values.
>>     KJ> I can't comment on the relationship with "S Programming"
>>
>> I can:
>>
>> In S, and S-plus upto version 3.4,
>> numeric constants such as '7' where  "double" as they are in R.
>>
>> Then in S-plus 5.1, they became "integer",
>> and there were tools so users could change all(!!) their S
>> scripts to use '7.' instead of '7' in all places where numeric
>> constants were seen, in order to keep behavior back compatible.
>>
>> R never made such a step (backwards ;-), and never will,
>> notably since in R we had introduced the explicit long (= long
>> integer) constants, using the 'L' suffix,
>> i.e.,  7L is "integer"
>>         7 is "double"
>>
>> Note however that for both, is.numeric(.) is fulfilled and
>> class(.) and mode(.) return "numeric".
>> Only typeof(.), storage.mode(.)  or  str(.)
>> (or functions building on these) tell you the difference.
>>
>> Martin Maechler, ETH Zurich and R core team
>>
>> [And, yes, if you think further and are wondering:
>>  If we'd design things from scratch, we would only have S4
>>  classes and "double" would be a proper class and
>>  "numeric" would be the class union of {"integer", "double"}
>> ]
>>
>>
>>     KJ> <ctu at bigred.unl.edu> wrote in message
>>     KJ> news:20080924000503.1fsyrqf6zokk40kg at wm-imp-1.unl.edu...
>>     >> This is really bothering me! In the Dr. Venables and Dr. 
>> Ripley's book  "S
>>     >> Programming" Page 105
>>     >> shows that
>>     >>> c(is(10,"integer"),is(10.5,"integer"))
>>     >> [1] T F
>>     >>
>>     >> But I try this in R 2.7.2 it shows
>>     >>> c(is(10,"integer"),is(10.5,"integer"))
>>     >> [1] FALSE FALSE
>>     >> Does anyone know what is going on here?
>>     >>
>>     >> Appreciate,
>>     >> Chunhao
>>     >>
>>     >> Quoting Yihui Xie <xieyihui at gmail.com>:
>>     >>
>>     >>> Yes, everyone will agree "7" is an integer, but I don't think
>>     >>> computers will agree too :-) R thinks it's a double-precision 
>> number,
>>     >>> except when you explicitly specify it as an integer (say,
>>     >>> as.integer()).
>>     >>>
>>     >>>> class(7)
>>     >>> [1] "numeric"
>>     >>>
>>     >>>> is.double(7)
>>     >>> [1] TRUE
>>     >>>
>>     >>> Regards,
>>     >>> Yihui
>>     >>> --
>>     >>> Yihui Xie <xieyihui at gmail.com>
>>     >>> Phone: +86-(0)10-82509086 Fax: +86-(0)10-82509086
>>     >>> Mobile: +86-15810805877
>>     >>> Homepage: http://www.yihui.name
>>     >>> School of Statistics, Room 1037, Mingde Main Building,
>>     >>> Renmin University of China, Beijing, 100872, China
>>     >>>
>>     >>>
>>     >>>
>>     >>> On Wed, Sep 24, 2008 at 12:40 PM,  <ctu at bigred.unl.edu> wrote:
>>     >>>> Hi R users
>>     >>>> Is there anything wrong in "is" function? (R 2.7.2)
>>     >>>> I believe that everyone will agree that "7" is an integer, 
>> right? but
>>     >>>> why R
>>     >>>> shows 7 is not an integer
>>     >>>>
>>     >>>>> is.integer(7)
>>     >>>>
>>     >>>> [1] FALSE
>>     >>>>>
>>     >>>>> is(7,"integer")
>>     >>>>
>>     >>>> [1] FALSE
>>     >>>>>
>>     >>>>> is(as.integer(7), "integer")
>>     >>>>
>>     >>>> [1] TRUE
>>     >>>>
>>     >>>> Thank you very much in advance
>>     >>>> Chunhao
>>
>>     KJ> ______________________________________________
>>     KJ> R-help at r-project.org mailing list
>>     KJ> https://stat.ethz.ch/mailman/listinfo/r-help
>>     KJ> PLEASE do read the posting guide 
>> http://www.R-project.org/posting-guide.html
>>     KJ> and provide commented, minimal, self-contained, reproducible 
>> code.
>>
>> ______________________________________________
>> 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.
>>
>
> ______________________________________________
> 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.
>



More information about the R-help mailing list