Forgive me for this unbeautiful code, but I’m trying to get it to work first. (“Of course this is only temporary. Unless it works” -red green)
I’m trying to create a function that will format columns of numbers in a style that I need them. It looks like this.
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')))))))))
}
And it works great for this:
> frmt(1413.612)
[1] "1 400"
But not this:
> 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
This is the code I finally got to work