[R] Why are integers coded as e.g. "2L" in R functions?
Prof Brian Ripley
ripley at stats.ox.ac.uk
Thu May 15 13:06:27 CEST 2014
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
--
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