R: why is "identical (c (1: 3), c (1, 2, 3))" false?

Why identical(c(1:3), c(1, 2, 3))false? In other words, why is the first an integer and the second a double?

+6
source share
2 answers
R> class(1:3)
[1] "integer"
R> class(c(1,2,3))
[1] "numeric"
R> 

In short, :since the sequence operator returns an integer, "because that's what people really want."

Hence:

R> identical(1:3, c(1L,2L,3L))
[1] TRUE
R> identical(1*(1:3), c(1,2,3))
[1] TRUE
R> 
+15
source

This is due to the colon operator. From ?':'or help(':'):

More details

The binary operator: has two meanings: for the coefficients a: b it is equivalent to the interaction (a, b) (but the levels are ordered and labeled differently).

from: to seq (from, to) from to to 1 -1. , fuzz 1e-7. (, ) .

- . integer, from , R , "double" (aka mode "numeric" ).

+5

Source: https://habr.com/ru/post/1015916/


All Articles