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)