完全删除整个R会话的科学记数法

时间:2012-05-30 19:50:11

标签: r options scientific-notation

想要在我的R会话中的任何计算结果中看到任何科学记数法。我希望看到所有实际数字,最好每三位数后用逗号(,)。

我该怎么做? options(scipen = 999)没有削减它。

1 个答案:

答案 0 :(得分:11)

print.default函数在“决定”要分配的宽度时查看options()['digits']值,因此您可能需要增加它以及尝试最大化“scipen”。存在操作系统特定问题,该宽度的值大于16。至于逗号请求,......忘了。 R不是财务报告系统。如果你需要强制使用这种输出格式,你可以将对象定义为特定的类,并使用sprintf和formatC为它们编写打印方法,或者我想你可以重写print.default,但这可能只会影响打印操作传递给print的其他100多种方法之一。

有输出方法。 formatC()prettyNum()都有一个'big.mark'参数,可以将逗号插入数字中。输出为"character"格式,因此尝试对您创建的结果进行任何进一步的计算。

还有一些输入法可以读取包含逗号或货币符号的数字的列:

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)