【问题标题】:Completely remove scientific notation for the entire R session [duplicate]完全删除整个 R 会话的科学记数法 [重复]
【发布时间】:2012-06-05 02:22:17
【问题描述】:

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

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

【问题讨论】:

  • 出于病态的好奇,为什么?双精度只能精确到约 16 位数字,因此之后的所有内容都会四舍五入。
  • 您了解控制台中显示的内容与写入文件或其他连接的内容之间的区别吗?

标签: r options scientific-notation


【解决方案1】:

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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-01
    • 2022-01-15
    • 2013-06-27
    相关资源
    最近更新 更多