Sai Sankalp Sai Sankalp - 1 year ago 53
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


die <- c(1:6)

OUTPUT1:-[1] "integer"


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

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 Source

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, 6), so you get a vector in integer storage mode.

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

# [1] "integer"

typeof(, 6))
# [1] "integer"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download