[R] Where are usages like "== 2L" documented?
Prof Brian Ripley
ripley at stats.ox.ac.uk
Tue Nov 17 16:04:19 CET 2009
On Tue, 17 Nov 2009, Bryan Hanson wrote:
> Patrick reminded me that it's also in The R Inferno, another document that I
> need to read again.
>
> So now, a more particular semi-question:
>
> x <- 1L
> y <- 1
> z <- 1.0
>
> class(x) # integer
> class(y) # numeric
> class(z) # numeric
NB: identical(x, y) is TRUE, so they behave the same.
> x == 1
> x == 1L
> y == 1
> z == 1 # all test TRUE
>
> Just to clarify, I think the steps above prove it, but *in a test* like x ==
> 1 the test is for the contents, not the storage mode, not for a combination
> of storage mode and contents.
Not really: the operands are coerced to a common type and then tested
for equality. From the help page for "=="
If the two arguments are atomic vectors of different types, one is
coerced to the type of the other, the (decreasing) order of
precedence being character, complex, numeric, integer, logical and
raw.
> So... The reason for defining the notion of "L" is smaller storage space,
> and more generally, for use anytime one wants explictly an integer for
> whatever reason? Are there other reasons, for instance, ways it saves lines
> of code?
It avoids coercion. The test x == 1 requires x to be coerced from
integer to double, and foo[1] requires '1' to be coerced from double
to integer. There is little if any gain in storage space for small
integer vectors over small double vectors, but all those coercions can
create a lot of new objects which need to be garbage-collected.
Also, 2L is simpler to write and to read than as.integer(2) (and
avoids a funtion call and a coercion).
>
> Bryan
> *************
> Bryan Hanson
> Acting Chair
> Professor of Chemistry & Biochemistry
> DePauw University, Greencastle IN USA
>
>
>
> On 11/17/09 4:20 AM, "Patrick Burns" <pburns at pburns.seanet.com> wrote:
>
>> 'The R Inferno' page 75.
>>
>>
>>
>> Patrick Burns
>> patrick at burns-stat.com
>> +44 (0)20 8525 0696
>> http://www.burns-stat.com
>> (home of "The R Inferno" and "A Guide for the Unwilling S User")
>>
>> Bryan Hanson wrote:
>>> Gurus:
>>>
>>> I keep seeing other people¹s code that contain ideas like
>>>
>>> If (x == 2L)
>>> X[-1L]
>>> X - 1L
>>>
>>> I have some idea of what¹s going on, but where is the use of concepts like
>>> ³2L² documented?
>>>
>>> Thanks, Bryan
>>> *************
>>> Bryan Hanson
>>> Acting Chair
>>> Professor of Chemistry & Biochemistry
>>> DePauw University, Greencastle IN USA
>>>
>>> ______________________________________________
>>> 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.
>
--
Brian D. Ripley, ripley at stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UK Fax: +44 1865 272595
More information about the R-help
mailing list