Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in R Programming by (5.3k points)

I often have seen the symbol 1L (or 2L, 3L, etc) appear in R code. What's the difference between 1L and 1?  1==1L evaluates to TRUE. Why is 1L used in R code?

1 Answer

0 votes
by
edited by

The suffix ‘L’ is used to specify a numeric value as an integer explicitly. You can also use the as.integer() function to specify an integer type, rather than a double that the standard numeric class is.

Sometimes you can use it to make your code run faster and consume less memory. A double ("numeric") value uses 8 bytes of memory. An integer value uses only 4 bytes of memory. For large vectors, that ensures less memory wastage and less to wade through for the CPU.

For example:

lt <- list(1,2.5,3L,6)

lapply(lt, class)

[[1]]

[1] "numeric"

[[2]]

[1] "numeric"

[[3]]

[1] "integer"

[[4]]

[1] "numeric"

To check memory consumption:

x <- 1:1000 

typeof(x)

[1] "integer" 

y <- x+1

typeof(y)

[1] "double"

object.size(y) 

8048 bytes

z <- x+1L

typeof(z)

[1] "integer" 

object.size(z) 

4048 bytes

Browse Categories

...