[R] Unclear documentation on 'numeric' and 'integer' (R-lang.pdf)

0 views
Skip to first unread message

Peng Yu

unread,
Jan 20, 2010, 5:37:56 PM1/20/10
to R-c...@r-project.org, r-h...@stat.math.ethz.ch
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?

> 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"
>

______________________________________________
R-h...@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.

Steve Lianoglou

unread,
Jan 20, 2010, 5:50:38 PM1/20/10
to Peng Yu, r-h...@stat.math.ethz.ch, R-c...@r-project.org
Hi Peng,

On Wed, Jan 20, 2010 at 5:37 PM, Peng Yu <peng...@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.

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

Reply all
Reply to author
Forward
0 new messages