强制R输出为科学计数法,最多两位小数 [英] Forcing R output to be scientific notation with at most two decimals

查看:588
本文介绍了强制R输出为科学计数法,最多两位小数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为特定的R脚本提供一致的输出。在这种情况下,我希望所有数值输出都用科学计数法精确地保留两位小数。

I would like to have consistent output for a particular R script. In this case, I would like all numeric output to be in scientific notation with exactly two decimal places.

示例:

0.05 --> 5.00e-02
0.05671 --> 5.67e-02
0.000000027 --> 2.70e-08

我尝试使用以下选项:

options(scipen = 1)
options(digits = 2)

这给了我结果:

0.05 --> 0.05
0.05671 --> 0.057
0.000000027 --> 2.7e-08

尝试时获得了相同的结果:

I obtained the same results when I tried:

options(scipen = 0)
options(digits = 2)

谢谢您的任何建议。

推荐答案

我认为最好使用 formatC 而不是更改全局设置。

I think it would probably be best to use formatC rather than change global settings.

对于您的情况,可能是:

For your case, it could be:

numb <- c(0.05, 0.05671, 0.000000027)
formatC(numb, format = "e", digits = 2)

哪个收益率:

[1] "5.00e-02" "5.67e-02" "2.70e-08"

这篇关于强制R输出为科学计数法,最多两位小数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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