R 的文档或语言定义是否说明了 R 整数的预期用途?

Does the documentation or language definition for R remark on the intended usage of R's integers?

我看到的关于 R 的整数类型的一个常见评论是,它实际上仅用于与 C 代码进行通信。 R 文档的任何官方部分中是否出现过这样的声明?我经常发现自己在制作像 integer(10) 这样的向量时,以为它们会更有效地满足我的目的,只是为了记住这个民间传说并重新考虑我是否应该将整数用于从不尝试与 C 代码通信的代码.

我不这么认为。这个民间传说可能是因为 R 在输入和强制转换方面相当宽松,所以很容易意外地得到一个浮点变量。

整数类型确实节省内存:

> object.size(seq(1e8))
400000048 bytes
> object.size(seq(1e8)+0.1)
800000048 bytes

我还没有尝试过基准测试来查看 R 是否对整数和浮点运算使用更快的例程,但你可以。

我没有仔细查看 all 的 R 文档,但在 R language definition 是:

In most cases,the difference between an integer and a numeric value will be unimportant as R will do the right thing when using the numbers. There are, however, times when we would like to explicitly create an integer value for a constant. We can do this by calling the function as.integer or using various other techniques ...

我在 R 源代码树的 doc/manual 目录中做了一个 grep integer *.texi 并且没有(快速浏览)注意到任何看起来相关的东西。

Ben Bolker's advice, I checked the seven R manuals. In addition to 之后,我发现了以下内容:

For most purposes the user will not be concerned if the “numbers” in a numeric vector are integers, reals or even complex. Internally calculations are done as double precision real numbers, or double precision complex numbers if the input data are complex.
An Introduction to R Section 2.2

Writing R Extensions 提供了很多使 R 与 C 和 Fortran 通信的指导,但它没有说明整数类型的意图。

最后一个要检查的地方是Full Reference Manual. You would have to be mad to do so - the word "integer" occurs over 1000 times. However, a quick look at the index reveals the documentation for the integer class。这给了我们答案是如此简单的英语,我不应该错过它:

Integer vectors exist so that data can be passed to C or Fortran code which expects them, and so that (small) integer data can be represented exactly and compactly.