我运行给定的代码:
> mean(as.numeric(x <- 1:4))
[1] 2.5
> class(x)
[1] "integer"
>
> x <- 2:5
> class(x)
[1] "integer"
> as.numeric(x)
[1] 2 3 4 5
> class(x)
[1] "integer"
>
查询 - 据我研究过一个行为像integer
的对象,最后必须分配L
,但在这里,我看到了完全不同的故事。那么,为什么x
和y
的类不是numeric
?
然而,如果没有矢量,事情就像往常一样:
> a <-3
> class(a)
[1] "numeric"
> b <- 3L
> class(b)
[1] "integer"
答案 0 :(得分:0)
如果我们检查?":"
,则已经说明了
对于数字参数,是一个数字向量。这将是整数类型 如果from是整数值,则结果可在R中表示 整数类型,否则为“double”类型(又名模式“numeric”)。
此处,2
和5
是整数,因此序列也将integer
调用。当然,类型推广是动态语言而不是静态语言
另外,检查
的输出class(seq(2, 5))
#[1] "integer"
class(seq(2.0, 5.0))
#[1] "integer"
class(seq(2.0, 5.0, by = 1.0))
#[1] "numeric"
class(seq(2, 5, by = 1.0))
#[1] "numeric"
class(seq(2, 5, by = 1))
#[1] "numeric"
class(seq(2, 5, by = 1L))
#[1] "numeric"