I often write R code where I test the length of a vector, the number of rows in a data frame, or the dimensions of a matrix, for example if (length(myVector) == 1)
. While poking around in some base R code, I noticed that in such comparisons values are explicitly stated as integers, usually using the 'L' suffix, for example if (nrow(data.frame) == 5L)
. Explicit integers are also sometimes used for function arguments, for example these statements from the cor
function: x <- matrix(x, ncol = 1L)
and apply(u, 2L, rank, na.last = "keep")
. When should integers be explicitly specified in R? Are there any potentially negative consequences from not specifying integers?
我经常编写R代码来测试向量的长度,数据帧中的行数或矩阵的维数,例如if(length(myVector)== 1)。在一些基本的R代码中,我注意到在这种比较中,值明确地表示为整数,通常使用'L'后缀,例如if(nrow(data.frame)== 5L)。显式整数有时也用于函数参数,例如来自cor函数的这些语句:x < - matrix(x,ncol = 1L)和apply(u,2L,rank,na.last =“keep”)。什么时候应该在R中明确指定整数?不指定整数会产生任何潜在的负面后果吗?
2 个解决方案
#1
2
Using 1L
etc is programmatically safe, as in it is explicit as to what is meant, and does not rely on any conversions etc.
使用1L等在程序上是安全的,因为它明确指出是什么意思,并且不依赖于任何转换等。
When writing code interactively, it can be easy to notice errors and fix along the way, however if you are writing a package (even base R
), it will be safer to be explicit.
以交互方式编写代码时,很容易发现错误并在此过程中进行修复,但是如果您正在编写程序包(甚至是基本R),那么显式更安全。
When you are considering equality, using floating point numbers will cause precision issues See this FAQ.
在考虑相等性时,使用浮点数会导致精度问题请参阅此常见问题解答。
Explicitly specifying integers avoids this, as nrow
and length
, and the index arguments to apply
return or require integers.
显式指定整数可以避免这种情况,如nrow和length,以及要应用return或者需要整数的索引参数。
#2
6
You asked:
你问:
Are there any potentially negative consequences from not specifying integers?
不指定整数会产生任何潜在的负面后果吗?
There are situations where it is likely to matter more. From Chambers Software for Data Analysis p193:
在某些情况下,它可能更重要。来自钱伯斯软件数据分析p193:
Integer values will be represented exactly as "double" numbers so long as the absolute value of the integer is less than 2^m, the length of the fractional part of the representation (2^54 for 32-bit machines).
只要整数的绝对值小于2 ^ m(表示的小数部分的长度)(32位机器为2 ^ 54),整数值将精确表示为“双”数。
It's not hard to see how if you calculated a value it might look like an integer but not quite be one:
不难看出如果你计算一个它可能看起来像一个整数但不是一个整数的值:
> (seq(-.45,.45,.15)*100)[3]
[1] -15
> (seq(-.45,.45,.15)*100)[3] == -15L
[1] FALSE
However, it's harder to come up with an example of explicitly typing in an integer and having it come up not quite an integer in the floating point representation, until you get into the larger values Chambers describes.
但是,更难以提出一个明确键入一个整数并在浮点表示中出现不是一个整数的例子,直到你进入钱伯斯所描述的更大的值。
#1
2
Using 1L
etc is programmatically safe, as in it is explicit as to what is meant, and does not rely on any conversions etc.
使用1L等在程序上是安全的,因为它明确指出是什么意思,并且不依赖于任何转换等。
When writing code interactively, it can be easy to notice errors and fix along the way, however if you are writing a package (even base R
), it will be safer to be explicit.
以交互方式编写代码时,很容易发现错误并在此过程中进行修复,但是如果您正在编写程序包(甚至是基本R),那么显式更安全。
When you are considering equality, using floating point numbers will cause precision issues See this FAQ.
在考虑相等性时,使用浮点数会导致精度问题请参阅此常见问题解答。
Explicitly specifying integers avoids this, as nrow
and length
, and the index arguments to apply
return or require integers.
显式指定整数可以避免这种情况,如nrow和length,以及要应用return或者需要整数的索引参数。
#2
6
You asked:
你问:
Are there any potentially negative consequences from not specifying integers?
不指定整数会产生任何潜在的负面后果吗?
There are situations where it is likely to matter more. From Chambers Software for Data Analysis p193:
在某些情况下,它可能更重要。来自钱伯斯软件数据分析p193:
Integer values will be represented exactly as "double" numbers so long as the absolute value of the integer is less than 2^m, the length of the fractional part of the representation (2^54 for 32-bit machines).
只要整数的绝对值小于2 ^ m(表示的小数部分的长度)(32位机器为2 ^ 54),整数值将精确表示为“双”数。
It's not hard to see how if you calculated a value it might look like an integer but not quite be one:
不难看出如果你计算一个它可能看起来像一个整数但不是一个整数的值:
> (seq(-.45,.45,.15)*100)[3]
[1] -15
> (seq(-.45,.45,.15)*100)[3] == -15L
[1] FALSE
However, it's harder to come up with an example of explicitly typing in an integer and having it come up not quite an integer in the floating point representation, until you get into the larger values Chambers describes.
但是,更难以提出一个明确键入一个整数并在浮点表示中出现不是一个整数的例子,直到你进入钱伯斯所描述的更大的值。