Is there a R function (or any package) allowing to format numbers (integer) using standard unit prefix (Kilo, Mega etc ...), so
10 -> 10
100 -> 1K
0.01
This is simple to vectorise using case_when from dplyr, and it's much easier on the eyes:
library(dplyr)
si_number = function(x, digits) {
compress = function(x, n) {
signif(x * 10^(-n), digits)
}
case_when(
x >= 1e6 ~ paste0(compress(x, 6), "M"),
x >= 1000 ~ paste0(compress(x, 3), "k"),
x >= 1 ~ as.character(compress(x, 0)),
x >= 0.001 ~ paste0(compress(x, -3), "m"),
x >= 1e-6 ~ paste0(compress(x, -6), "u")
)
}