请原谅我这个不太漂亮的代码,但我想让它先工作。 (“当然这只是暂时的。除非它有效” - 绿色)
我正在尝试创建一个函数,它将以我需要的样式格式化数字列。它看起来像这样。
frmt <- function(x, rates=FALSE, thou=FALSE) {
ifelse(x==0, "0",
ifelse(x < 0.01 & thou==TRUE, "<0.01",
ifelse(x < 0.1 & thou==FALSE, "<0.1",
ifelse(signif(x, 2) < 1 & thou==TRUE, formatC(round(x,3), format='f', digits=3),
ifelse(signif(x, 2) < 1, formatC(round(x,2), format='f', digits=2),
ifelse(signif(x, 2) < 10, formatC(round(x,2), format='f', digits=1),
ifelse(rates==FALSE, formatC(signif(x, 2), big.mark=" ", format='d'),
ifelse(signif(x, 3) < 100, formatC(signif(x, 2), big.mark=" ", format='d'), formatC(signif(x, 3), big.mark=" ", format='d')))))))))
}
这对此非常有用:
> frmt(1413.612)
[1] "1 400"
但不是这样:
> frmt(c(0.0001, 0.0213, 0.25413, 1.0123, 9.9123, 15.124, 112.05, 1413.612, NA))
[1] "<0.1" "<0.1" "0.25" "1.0" "9.9" "0" "0" "0" NA
答案 0 :(得分:0)
这是我最终开始工作的代码
frmt <- function(x, rates=FALSE, thou=FALSE) {
ifelse(x==0, "0",
ifelse(x < 0.01 & thou==TRUE, "<0.01",
ifelse(x < 0.1 & thou==FALSE, "<0.1",
ifelse(signif(x, 2) < 1 & thou==TRUE, formatC(round(x,3), format='f', digits=3),
ifelse(signif(x, 2) < 1, formatC(round(x,2), format='f', digits=2),
ifelse(signif(x, 2) < 10, formatC(round(x,2), format='f', digits=1),
ifelse(x > 1 & rates==FALSE, formatC(signif(x, 2), big.mark=" ", format='d'),
ifelse(signif(x, 3) < 100, formatC(signif(x, 2), big.mark=" ", format='d'), formatC(signif(x, 3), big.mark=" ", format='d')))))))))
}