【发布时间】:2016-04-19 14:38:05
【问题描述】:
numeric 类包括子类integer 和double。有趣的是,变量的类取决于它的初始化方式。例如:
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
我的问题:为什么 R 在示例中不强制 x 对整数进行分类?我认为这样做更有意义,因为x 是integer 和numeric 向量,但numeric 向量不一定是整数向量。因此,强制x 成为整数类可能更直观,至少对我而言。我错过了什么吗?
【问题讨论】: