x
and y
below are not quite identical because they have different storage modes, as you discovered by using str(x)
and str(y)
. In my experience, this distinction is unimportant 99% of the time; R uses fairly loose typing, and integers will automatically be promoted to double (i.e. double-precision floating point) when necessary. Integers and floating point values below the maximum integer value (.Machine$integer.max
) can be converted back and forth without loss of information. (Integers do take slightly less space to store, and can be slightly faster to compute with as @RichardScriven comments above.)
If you want to create an integer vector, append L
as below ... or use as.integer()
as suggested in comments above.
x <- 1:5
y <- c(1,2,3,4,5)
z <- c(1L,2L,3L,4L,5L)
all.equal(x,y) ## test for _practical_ equivalence: TRUE
identical(x,y) ## FALSE
identical(x,z) ## TRUE
storage.mode()
and class()
may also be useful, as well as is.integer()
, is.double()
, as.integer()
, as.double()
, is.numeric()
...