我有一个存储美元金额的数据框,它看起来像这样
> a
cost
1 1e+05
2 2e+05
我希望它可以显示为这样
> a
cost
1 $100,000
2 $200,000
在 R 中如何做到这一点?
DF <- data.frame(cost=c(1e4, 2e5))
#assign a class
oldClass(DF$cost) <- c("money", oldClass(DF$cost))
#S3 print method for the class
print.money <- function(x, ...) {
print.default(paste0("$", formatC(as.numeric(x), format="f", digits=2, big.mark=",")))
}
#format method, which is necessary for formating in a data.frame
format.money <- function(x, ...) {
paste0("$", formatC(as.numeric(x), format="f", digits=2, big.mark=","))
}
DF
# cost
#1 $10,000.00
#2 $200,000.00
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)