在R中使用科学记数法 [英] using scientific notation in R
问题描述
我目前使用 printCoefmat
来打印出一个矩阵,并希望对这些数字应用一些格式。
当数字的指数大于3时,我想强制使用科学记数法。
我无法弄清楚 scipen
是如何工作的,有没有人有想法如何我可以做到这一点?
只要键入一个大数字来让R显示不科学的符号。
options(scipen = 20)
如果这还不够,请将号码放大一些。
scipen惩罚是如何工作的?
令人困惑的是,处罚适用于科学记数法版本,因为R会查看打印特定字符串所需的字符数。它将科学记数法中的字符数加上 scipen
惩罚值,如果它仍然小于打印实际数字所需的字符数,那么它将打印科学反之亦然。我希望这个例子可以说明这一点:$ b
$ b $ $ $ $ $ $ $ $ $ $ $ $ $ options $($ = b $ b> 1e5
#[1] 1e + 05 ---->科学中的5个字符,而正常时为'100000'的6个字符
> 1e4
#[1] 10000 ----> (scipen = 1)
> 1e5
#[1] 100000 ---->正常情况下5个字符,而科学
选项中的'1e + 04'正常情况下为6个字符,而科学
的科目为'1e + 05'+ scipen惩罚与5 + 1
I'm currently using printCoefmat
to print a matrix out and want to apply some formatting to the numbers.
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?
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...
How does the scipen penalty work?
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屋!