Sai Sankalp Sai Sankalp - 3 months ago 5
R Question

R- Difference betwen c(1:6) and c(1,2,3,4,5,6) in the case of typeof() function?

I know that R will save any number that we type as "double" by default. But if I run the following codes, it is giving me two different answers

CODE 1

die <- c(1:6)
typeof(die)


OUTPUT1:-[1] "integer"

CODE 2

die <- c(1,2,3,4,5,6)
typeof(die)


OUTPUT2:-[2] "double"

So Please clarify me why this "integer" if I use c(1:6) and "double" if directly specify all the elements in the list?

Answer

If you do c(1L, 2L, 3L, 4L, 5L, 6L), you will get "integer" rather than "double". Just compare tyoeof(1) and typeof(1L). Or you may read my answer here: How does is.integer work?.

1:6 is short for seq.int(1, 6), so you get a vector in integer storage mode.

typeof(c(1L, 2L, 3L, 4L, 5L, 6L))
# [1] "integer"

typeof(1:6)
# [1] "integer"

typeof(seq.int(1, 6))
# [1] "integer"