Why vectors are created by an integer ::

Why are vectors created using a :type integerand vectors created using a c()type double?

a <- 1:7
typeof(a)
# "integer"
class(a)
# "integer"
b <- c(1,2,3,4,5,6,7)
typeof(b)
# "double"
class(b)
# "numeric"
+4
source share
1 answer

Does not quite answer why , but tries to shed light on what .
Let's start by creating some sequences of numbers:

x <- 1:10
z <- c(1,2,3,4,5,6,7,8,9,10)
typeof(x)
# [1] "integer"
typeof(z)
# [1] "double"
class(x)
# [1] "integer"
class(z)
# [1] "numeric"

As @docendodiscimus noted, they may look the same, but they are different. This is important if you are checking if they are identical:

identical(x,z)
# [1] FALSE
x == z
# [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE

== " , ". (. ?==), identical() - " , . TRUE FALSE ". (. ?identical).

, seq()

y1 <- seq(1,10) # which is the equivalent to 1:10 (see ?`:`)
y2 <- seq(1,10, by = 1)
typeof(y1)
# [1] "integer"
typeof(y2)
# [1] "double"
class(y1)
# [1] "integer"
class(y2)
# [1] "numeric"

by = ... 1 , .

, , ( ). help ,

" , " [ , ]

mode numeric

mode(y1)
# [1] "numeric"
mode(y2)
# [1] "numeric"
mode(x)
# [1] "numeric"
mode(z)
# [1] "numeric"

The R Inferno ( ).

, z

z <- as.integer(c(1,2,3,4,5,6,7,8,9,10))
typeof(z)
# [1] "integer"
class(z)
# [1] "integer"
identical(x,z)
# [1] TRUE
+4

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


All Articles