rintegercinterop

Does the documentation or language definition for R remark on the intended usage of R's integers?


A common comment that I see made about R's integer type is that it's only really intended for communication with C code. Do any statement like this appear in any official part of R's documentation? I often catch myself making vectors like integer(10) under the impression that they'll be more efficient for my purposes, only to remember this folklore and reconsider if I should ever be using integers for code that never tries to communicate with C code.


Solution

  • I don't think so. This folklore probably comes from the fact that R is pretty loose about typing and coercion, so it's easy to end up with a floating-point variable by accident.

    Integer types certainly save memory:

    > object.size(seq(1e8))
    400000048 bytes
    > object.size(seq(1e8)+0.1)
    800000048 bytes
    

    I haven't tried benchmarking to see if R uses faster routines for integer vs floating-point arithmetic, but you could.

    I haven't looked carefully through all of R's documentation, but the only slightly relevant comment that turns up in a full-text search for "integer" in the R language definition is:

    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. There are, however, times when we would like to explicitly create an integer value for a constant. We can do this by calling the function as.integer or using various other techniques ...

    I did a grep integer *.texi in the doc/manual directory of the R source tree and didn't (in a quick skim) notice anything else that looked relevant.