R中的整数与数字数据类型

时间:2015-11-05 17:50:09

标签: r vector integer numeric

This is a question I had while going through R programming course from Coursera. I had asked this question in their forums, but didn't get any answer.

所以我想,我应该在这里问一下。

据我了解教授在该讲座中所讨论的内容 - 默认情况下,当我们将任何数值存储在变量中时如下所示

x <- 1
x

# prints numeric
class(x)

但为什么当我们存储如下所示的向量时(注意:仍然没有&#39; L&#39;符号强制它为整数)

x <- 1:10
x

# prints "integer", but why?
class(x)

我认为它应该给我一个数字向量,但事实并非如此。

有人可以解释一下这里发生了什么吗?

1 个答案:

答案 0 :(得分:2)

已对此进行了讨论,请参阅http://r.789695.n4.nabble.com/Integer-vs-numeric-td847329.html 来自help(":")

Value:

     For numeric arguments, a numeric vector.  This will be of type
     'integer' if 'from' is integer-valued and the result is 
     representable in the R integer type, otherwise of type '"double"'
     (aka 'mode' '"numeric"').