简体   繁体   English

在 R 中将千位格式化为 Ks

[英]Format thousand to Ks in R

How to format numbers like 465456.6789 to beautiful 465,4K in R?如何在 R 中将 465456.6789 等数字465456.6789为漂亮的465,4K Other examples 13567.566 to 13,5K 3567.5 to 3,5K and so on.其他示例 13567.566 到 13,5K 3567.5 到 3,5K 等等。 In general I want something like一般来说,我想要类似的东西

roundup_to <- function(x, to = 10, up = FALSE){
  if(up) round(.Machine$double.eps^0.5 + x/to)*to else round(x/to)*to
}

roundup_to(c((74453.867574737)), to = 100)

to become 74,5K成为74,5K

You could do:你可以这样做:

a <- c(465456.6789, 13567.566, 3567.5)
sprintf("%sK", format(round(a/1000, 1), dec=","))
[1] "465,5K" " 13,6K" "  3,6K"

You can also have a look at function scales::label_number_si which rounds the number.您还可以查看对数字进行四舍五入的 function scales::label_number_si

a <- c(465456.6789, 3567.5, 1465458.12)
scales::label_number_si(accuracy = 0.1)(a)

#[1] "465.5K" "3.6K"   "1.5M"  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM