Alexander Engelhardt
2014-May-15 10:54 UTC
[R] Why are integers coded as e.g. "2L" in R functions?
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? Thanks in advance, Alex
Rui Barradas
2014-May-15 11:03 UTC
[R] Why are integers coded as e.g. "2L" in R functions?
Hello, 2 is a double, 2L is an integer. Try > class(2) [1] "numeric" > class(2L) [1] "integer" Hope this helps, Rui Barradas Em 15-05-2014 11:54, Alexander Engelhardt escreveu:> 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? > > Thanks in advance, > Alex > > ______________________________________________ > 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.
Hi Alex, May be this link helps: http://stackoverflow.com/questions/7014387/whats-the-difference-between-1l-and-1 A.K. On Thursday, May 15, 2014 6:55 AM, Alexander Engelhardt <alex at chaotic-neutral.de> 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? Thanks in advance, ? Alex ______________________________________________ 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.
Pascal Oettli
2014-May-15 11:05 UTC
[R] Why are integers coded as e.g. "2L" in R functions?
Hello, Probably you will get more complete answer, but you can compare "class(1)" with "class(1L)". Regards, Pascal On Thu, May 15, 2014 at 7:54 PM, Alexander Engelhardt <alex at chaotic-neutral.de> 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? > > Thanks in advance, > Alex > > ______________________________________________ > 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.-- Pascal Oettli Project Scientist JAMSTEC Yokohama, Japan
Prof Brian Ripley
2014-May-15 11:06 UTC
[R] Why are integers coded as e.g. "2L" in R functions?
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