在 R 中使用科学记数法 [英] using scientific notation in R

查看:33
本文介绍了在 R 中使用科学记数法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用 printCoefmat 来打印一个矩阵并希望对数字应用一些格式.

I'm currently using printCoefmat to print a matrix out and want to apply some formatting to the numbers.

当数字的指数大于 3 时,我想强制使用科学记数法.我不太明白 scipen 是如何工作的,有人知道我该怎么做吗?

I want to force scientific notation when the numbers have an exponent greater than 3. I can't quite figure out how scipen works, Does anyone have any idea how I can do this?

推荐答案

只要输入一个大数字就可以让R显示不科学的符号.

Just type in a big number to get R to display unscientific notation.

options( scipen = 20 )

如果这还不够,请将数字变大...

If that's not enough, make the number bigger...

这很令人困惑,但是 penalty 应用于科学记数法版本,因为在 R 中查看打印特定字符串需要多少个字符.它将值 scipen 惩罚添加到科学计数法的字符数中,如果它仍然小于打印实际数字所需的字符数,那么它将打印科学计数法,反之亦然.我希望这个例子能说明这一点:

It is confusing, but the penalty is applied to the scientific notation version, as in R looks at how many characters it takes to print a particular string. It adds the value scipen penalty to the number of characters in scientific notation and if it is still less than the number of characters required to print the actual number then it will print scientific and vice versa. I hope this example will illustrate the point:

options( scipen = 0 )
options( digits = 6 )
>1e5
#[1] 1e+05    ----> 5 characters in scientific, vs. 6 for '100000' in normal
>1e4
#[1] 10000    ----> 5 characters in normal, vs. 5 for '1e+04' in scientific
options(scipen = 1 )
>1e5
#[1] 100000    ----> 6 characters in normal, vs. 5 + 1 for '1e+05' + scipen penalty in scientific

这篇关于在 R 中使用科学记数法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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