[R] Why are integers coded as e.g. "2L" in R functions?

Alexander Engelhardt alex at chaotic-neutral.de
Thu May 15 13:57:59 CEST 2014


So in essence, it always provides the same output, but saves space and 
speed.

Thanks everyone!
  - Alex

On 05/15/2014 01:06 PM, Prof Brian Ripley wrote:
> On 15/05/2014 11:54, Alexander Engelhardt wrote:
>> Hello R-help,
>>
>> I keep noticing R functions that don't compare integers like
>>    if(x == 2)
>> but instead
>>    if(x == 2L)
>>
>> Is this a long integer? Also, when do the two notations have a different
>> effect on the code?
>
> 2L is an integer. both in the mathematical sense and the sense of
> typeof().  2 is a double with a value that is a mathematical integer.
> They are stored differently, and in x == 2 'x' will be coerced to
> double, often unnecessarily.  Which is why careful authors write e.g.
>
> length(x) == 2L
>
> as length() the vast majority of the time gives an integer (especially
> where length-2 is expected).
>
>> Thanks in advance,
>>   Alex
>
>



More information about the R-help mailing list