原谅我这个不漂亮的代码,但我试图让它首先工作。(“当然这只是暂时的。除非它有效”-red green)
我正在尝试创建一个函数,它将以我需要的样式格式化数字列。它看起来像这样。
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