#StackBounty: #r #function #integer #numeric #interactive Benefits of using of integer values for constants rather than numeric values …

Bounty: 50

In R source code, most (but not all) functions use integer values for constants:

colnames <- function(x, do.NULL = TRUE, prefix = "col")
{
    if(is.data.frame(x) && do.NULL)
    return(names(x))
    dn <- dimnames(x)
    if(!is.null(dn[[2L]]))
    dn[[2L]]
    else {
        nc <- NCOL(x)
    if(do.NULL) NULL
        else if(nc > 0L) paste0(prefix, seq_len(nc))
        else character()
    }
}

R Language Definition says:

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.

  • What are these cases where there is a need to force integer values for constants instead of simply using numeric values? Examples where e.g., 1 would fail but e.g., 1L would not are welcome.
  • Conversely, in which cases using integer values is not necessary (e.g., interactive use vs programming, indexing with constants, etc.)?

The questions are about good practice and the rationale, not about e.g. the "L" notation itself, the difference between integer class and numeric class, or comparing numbers.


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.