Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
704 views
in Technique[技术] by (71.8m points)

format - using scientific notation in R

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...