Clarification of L in R

2020-01-29 05:18发布

My trail on L in R is:

c<-1:10
c
# [1]  1  2  3  4  5  6  7  8  9 10
c[-1]
# [1]  2  3  4  5  6  7  8  9 10
c[-2]
# [1]  1  3  4  5  6  7  8  9 10
c[-1L]
# [1]  2  3  4  5  6  7  8  9 10
c[-2L]
# [1]  1  3  4  5  6  7  8  9 10

I tried using ?L without success.

What indeed is x[<n>L]? Any example for further usage of it?

1条回答
唯我独甜
2楼-- · 2020-01-29 05:40

This answer is a summary of the comments above. It is basically just pointers to various help texts, but as evident from OP's attempt with ?L, it is not always easy to find the relevant help text. I was expecting to find something about L in ?as.integer, but no. Hopefully this answer is more useful than a pile of comments.

  • In the R language definition you will find: "We can use the L suffix to qualify any number with the intent of making it an explicit integer"
  • From ?NumericConstants: "[...] All other numeric constants start with a digit or period and are either a decimal or hexadecimal constant optionally followed by L"

    "An numeric constant immediately followed by L is regarded as an integer number when possible (and with a warning if it contains a ".")."

    "You can combine the "0x" prefix with the "L" suffix".

  • You may also find it useful to check the examples on floating point vs. integers in the section "Two Kinds Revisited" here. "Put capital L (as in “long”) after a number to make R create it as an integer".
  • Not specifically about L, but always relevant in the floating point vs. integers context is FAQ7.31: "Why doesn’t R think these numbers are equal?".

Threads with discussions about the efficiency of L:

Threads on R-help where others have struggled to find documentation about L, with a possible explanation of why the letter L, and why L vs as.integer in terms of efficiency.

  1. Difference between 10 and 10L

    First William Dunlap:

    Why not 10I for integer? Perhaps because "I" and "l" look too similar, perhaps because "i" and "I" sound too similar. The "L" does not mean "long": integers are 4 bytes long.

    Then Brian Ripley:

    Actually it does: this notation dates from the C language on 16-bit computers where integers were 16-bits and longs were 32-bit (and R has no 'long' type).

    The author of this in R never explained why he chose the notation, but it is shorter than as.integer(10), and more efficient as the coercion is done at parse time.

  2. The L Word
    Discussion about the efficiency in different situations, with some benchmarkings.

  3. R history: Why 'L; in suffix character ‘L’ for integer constants?

  4. More discussions here.

查看更多
登录 后发表回答