问题描述
考虑以下脚本:
list_of_numbers <- as.numeric()
for(i in 1001999498:1002000501){
list_of_numbers <- c(list_of_numbers, i)
}
write(list_of_numbers, file = "./list_of_numbers", ncolumns = 1)
生成的文件如下:
[user@pc ~]$ cat list_of_numbers
1001999498
1001999499
1.002e+09
...
1.002e+09
1.002e+09
1.002e+09
1002000501
我发现了另外两个范围,其中R不能始终如一地打印数字格式.
I found a couple more ranges where R does not print consistently the number format.
现在我有以下问题:
这是错误还是此行为的实际原因?为什么只在一定范围内,为什么不是x上的每个数字都没有?
Is this a bug or is there an actual reason for this behavior?Why just in certain ranges, why not every number above x?
我知道如何解决这个问题:
I know how I can solve this like this:
options(scipen = 1000)
但是,除了设置全局选项之外,还有其他更优雅的方法吗?无需将其转换为数据框并更改格式.
But are there more elegant ways than setting global options? Without converting it to a dataframe and changing the format.
推荐答案
这不是bug,R选择了最短的表示形式.
It's not a bug, R chooses the shortest representation.
更准确地说,在?options
中,可以读取:
More precisely, in ?options
one can read:
因此,当scipen
为0(默认值)时,首选最短的表示法.
So when scipen
is 0 (the default), the shortest notation is preferred.
请注意,使用format(x, scientific = TRUE)
可以得到数字x
的科学计数法.
Note that you can get the scientific notation of a number x
with format(x, scientific = TRUE)
.
在您的情况下:
-
1001999499
长10个字符,而科学符号1.001999e+09
长(12个字符),因此保留了十进制符号. -
1001999500
:科学符号为1.002e+09
,它更短. - .....................(科学计数法等于
1.002e+09
,因此更短) -
1002000501
:1.002001e+09
更长.
1001999499
is 10 characters long whereas its scientific notation1.001999e+09
is longer (12 characters), so the decimal notation is kept.1001999500
: scientific notation is1.002e+09
, which is shorter.- ..................... (scientific notation stays equal to
1.002e+09
, hence shorter) 1002000501
:1.002001e+09
is longer.
您可能会问:为什么1001999500
格式化为1.002e+09
而不是1.0019995e+09
?仅仅是因为还有一个选项可以控制有效数字的数量.它的名称为digits
,默认值为7.由于1.0019995
具有8个有效数字,因此将其四舍五入为1.002
.
You may ask: how come that 1001999500
is formatted as 1.002e+09
and not as 1.0019995e+09
? It's simply because there is also an option that controls the number of significant digits. It is named digits
and its default value is 7. Since 1.0019995
has 8 significant digits, it is rounded up to 1.002
.
确保不更改全局选项而保留十进制表示法的最简单方法是使用format
:
The simplest way to ensure that decimal notation is kept without changing global options is probably to use format
:
write(format(list_of_numbers, scientific = FALSE, trim = TRUE),
file = "./list_of_numbers")
侧面说明:您不需要循环即可生成您的list_of_numbers
(顺便说一句,它不是列表,而是向量).只需使用:
Side note: you didn't need a loop to generate your list_of_numbers
(which by the way is not a list but a vector). Simply use:
list_of_numbers <- as.numeric(1001999498:1002000501)
这篇关于函数write()与数字符号不一致的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!