函数write()与数字符号不一致 [英] Function write() inconsistent with number notation

查看:101
本文介绍了函数write()与数字符号不一致的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下脚本:

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,否则将首选

固定的表示法. 位数宽.

fixed notation will be preferred unless it is more than scipen digits wider.

因此,当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 notation 1.001999e+09 is longer (12 characters), so the decimal notation is kept.
  • 1001999500: scientific notation is 1.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()与数字符号不一致的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆