Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
468 views
in Technique[技术] by (71.8m points)

integer - Clarification of L in R

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

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

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

  3. More discussions here.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...