我需要使用这样的缩写来显示数字
1 shows as 1
999 shows as 999
1000 shows as 1K
999000 shows as 999K
1000000 shows as 1M
1500000 shows as 1.5M
1000000000 shows as 1G
etc...
我已经看到了所有其他语言(至少三次Java语言)在stackoverflow上提出的问题:
使用公制前缀(例如1.5K,1M,1G等)格式化javascript数字
...但是我找不到R的任何东西。我错过了什么吗?
使用dplyr::case_when
:
so_formatter <- function(x) {
dplyr::case_when(
x < 1e3 ~ as.character(x),
x < 1e6 ~ paste0(as.character(x/1e3), "K"),
x < 1e9 ~ paste0(as.character(x/1e6), "M"),
TRUE ~ "To be implemented..."
)
}
test <- c(1, 999, 1000, 999000, 1000000, 1500000, 1000000000, 100000000000)
so_formatter(test)
# [1] "1"
# [2] "999"
# [3] "1K"
# [4] "999K"
# [5] "1M"
# [6] "1.5M"
# [7] "To be implemented..."
# [8] "To be implemented..."
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句