[R] Unclear documentation on 'numeric' and 'integer' (R-lang.pdf)
Steve Lianoglou
mailinglist.honeypot at gmail.com
Wed Jan 20 23:50:38 CET 2010
Hi Peng,
On Wed, Jan 20, 2010 at 5:37 PM, Peng Yu <pengyu.ut at gmail.com> wrote:
> R-lang.pdf has the following description in Section 3.1.1.
>
> """
> Any number typed directly at the prompt is a constant and is evaluated.
>> 1
> [1] 1
> Perhaps unexpectedly, the number returned from the expression 1 is a
> numeric. In most
> cases, the difference between an integer and a numeric value will be
> unimportant as R will
> do the right thing when using the numbers.
> """
>
> I'm not sure if I understand it. According to the following example
> adding 'L' after '1', doesn't change the mode from 'numeric' to
> 'integer'. Could somebody clarify this and revise the document to make
> this point clearer?
An Integer is a subclass of 'numeric':
R> is(1L)
[1] "integer" "numeric" "vector"
"data.frameRowLabels"
R> is(1)
[1] "numeric" "vector"
So, right: adding the L doesn't make the number "not numeric", it just
specifies it to be an integer, which itself is also a numeric.
>
>> interogate<-function(f) {
> + print(f(1))
> + x=1
> + print(f(x))
> + x=1L
> + print(f(x))
> + print(f(1L))
> + print(f(as.integer(1)))
> + }
>>
>> interogate(typeof)
> [1] "double"
> [1] "double"
> [1] "integer"
> [1] "integer"
> [1] "integer"
>> interogate(mode)
> [1] "numeric"
> [1] "numeric"
> [1] "numeric"
> [1] "numeric"
> [1] "numeric"
>> interogate(storage.mode)
> [1] "double"
> [1] "double"
> [1] "integer"
> [1] "integer"
> [1] "integer"
Also, look at ?mode ... it should help to address your confusion as it
explicitly addresses the behavior you are observing.
-steve
--
Steve Lianoglou
Graduate Student: Computational Systems Biology
| Memorial Sloan-Kettering Cancer Center
| Weill Medical College of Cornell University
Contact Info: http://cbio.mskcc.org/~lianos/contact
More information about the R-help
mailing list