Difference Between 1 and 1L in R
In R, there are different ways to represent numerical and integer values.
The numerical values can be represented as both integers and decimals (both 1 and 1.0 are the same).
But in some cases, you need to explicitly create an integer value i.e. the value without any decimal places. For example, during programming, you may have to explicitly create an integer data type.
In R, you can create the integre value using the as.integer
method or by using other various methods. But the
simplest way to create an integer value is to add the suffix L
to the number (for example, 1L, 1e2L).
Adding suffix L
ensures that the value is treated as an integer and it is useful for memory usage and specific computations involving integer operations.
The following examples illustrating the difference between 1
and 1L
in R.
Create a numerical value,
# create numerical value
num_val <- 1
# check the data type
print(class(num_val))
[1] "numeric"
print(typeof(num_val))
[1] "double"
Create integer value (with suffix L
),
# create integer value
int_val <- 1L
# check the data type
print(class(int_val))
[1] "integer"
print(typeof(int_val))
[1] "integer"
In summary, 1 is a numeric constant of type double
, while 1L
is an integer constant of type integer
.