0

The class numeric includes sub-classes integer and double. The interesting thing is that the class of a variable depends on the way it is initialised. For example:

x <- c(0,0,0,0,0,1,1,1,1,1)
y <- rep(0:1, c(5,5))

x
# [1] 0 0 0 0 0 1 1 1 1 1
y
# [1] 0 0 0 0 0 1 1 1 1 1

class(x)
# [1] "numeric"
class(y)
# [1] "integer"

identical(x,y)
# [1] FALSE

My question: why does R not coerce x in the example to class integer? I think it would make more sense to do so as x is an integer and a numeric vector, but a numeric vector is not necessarily a vector of integers. Hence, coercing x to be of the integer class might be more intuitive, at least for me. Am I missing something?

1 Answers1

-1

When you type a 0 R understands this as a numeric (double). An integer has to be typed as 0L. And of course, : is documented to return integer values "if from is integer-valued and the result is representable in the R integer type".

Roland
  • 127,288
  • 10
  • 191
  • 288