问题
I do not want to see any scientific notation in any result of any calculation in my R session. I want to see all the actual numbers, preferably with a comma (,) after every three digits.
How can I do that? options(scipen = 999)
does not cut it.
回答1:
The print.default
function looks at the options()['digits']
value in "deciding" what width to allocate, so you may need to increase it as well as trying to max out 'scipen'. There are OS specific issues with values above 16 for that width. As for the commas request, ... forget it. R is not a financial reporting system. If you need to force this output format, you can define objects to be of a particular class and write print methods for them using sprintf and formatC, Or I suppose you can rewrite print.default, but that might only affect printing operations that were not passed to one of the other 100+ methods for print
.
There are output methods. formatC()
and prettyNum()
both have a 'big.mark' argument that will insert commas into numbers. The output is in "character"
format, so do not try to do any further calculations on the results you create.
There are also input methods that could read columns with numbers containing commas or currency symbols:
setAs("character", "num.with.commas", function(from) as.numeric(gsub(",", "", from)))
setAs("character", "euro",
function(from) as.numeric(gsub("€", "", from)))
setAs("character", "num_pct",
function(from) as.numeric(gsub("%", "", from))/100)
# you will get warning messages if you have not defined the class,
# .... but this will still succeed
Input <- "A B C
1,000 1% 3.50€
2,000 2% 4.77€
3,000 3% €5.68
"
DF <- read.table(textConnection(Input), header = TRUE,
colClasses = c("num.with.commas", "num_pct", "euro"))
str(DF)
来源:https://stackoverflow.com/questions/10823241/completely-remove-scientific-notation-for-the-entire-r-session