我和一大群人一起工作。我知道如何从:Comma separator for numbers in R?将数字转换为逗号格式。我不知道如何在控制台中用逗号显示数字,而不将类从数字中转换。我希望能够看到逗号,这样我就可以在工作时比较数字,但需要将数字保持为数字才能进行计算。我知道你可以从:How to disable scientific notation?中去掉科学符号,但是找不到逗号或美元格式的等价物。
发布于 2018-03-02 17:47:36
您可以为print()创建一个新方法,用于我将称为“bignum”的自定义类:
print.bignum <- function(x) {
print(format(x, scientific = FALSE, big.mark = ",", trim = TRUE))
}
x <- c(1e6, 2e4, 5e8)
class(x) <- c(class(x), "bignum")
x
[1] "1,000,000" "20,000" "500,000,000"
x * 2
[1] "2,000,000" "40,000" "1,000,000,000"
y <- x + 1
y
[1] "1,000,001" "20,001" "500,000,001"
class(y) <- "numeric"
y
[1] 1000001 20001 500000001对于任何数字对象x,如果通过class(x) <- c(class(x), "bignum")将"bignum“添加到类属性中,它将始终打印您所描述的要打印它的方式,但如果不是这样,则应该以数字的形式运行,如上面所示。
https://stackoverflow.com/questions/49073960
复制相似问题