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?
From the Constants Section of the R Language Definition: We can use the 'L' suffix to qualify any number with the intent of making it an explicit integer. So '0x10L' creates the integer value 16 from the hexadecimal representation.
Integer Datatype R supports integer data types which are the set of all integers. You can create as well as convert a value into an integer type using the as. integer() function. You can also use the capital 'L' notation as a suffix to denote that a particular value is of the integer data type.
In R integers are specified by the suffix L (e.g. 1L ), whereas all other numbers are of class numeric independent of their value. The function is. integer does not test whether a given variable has an integer value, but whether it belongs to the class integer .
Because R's integers are 32-bit long integers and "L" therefore appears to be sensible shorthand for referring to this data type.
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.
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.
The L Word
Discussion about the efficiency in different situations, with some benchmarkings.
R history: Why 'L; in suffix character ‘L’ for integer constants?
More discussions here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With