Reformarring complex factor vector with comma separation after thousand

做~自己de王妃 提交于 2019-12-02 05:15:39

Operating only on the levels seem to keep your precision level, not converting your vector to character vector and much more efficient as it is reducing the size of the data you operate on only to the unique values (rather the whole vector)

levels(vec) <- sapply(strsplit(levels(vec), " - "), 
                       function(x) paste(prettyNum(x, 
                                            big.mark = ",", 
                                            preserve.width = "none"), 
                                   collapse = " - "))
vec
#  [1] 0 - 100                            0 - 100                            0 - 100                            0 - 100                            150.22 - 170.33                   
#  [6] 1,000 - 2,000                      1,000 - 2,000                      1,000 - 2,000                      1,000 - 2,000                      7,000 - 10,000                    
# [11] 7,000 - 10,000                     7,000 - 10,000                     7,000 - 10,000                     7,000 - 10,000                     1,000,000 - 22,000,000            
# [16] 1,000,000 - 22,000,000             1,000,000 - 22,000,000             44,000,000 - 66,000,000.8989898989
# Levels: 0 - 100 150.22 - 170.33 1,000 - 2,000 7,000 - 10,000 1,000,000 - 22,000,000 44,000,000 - 66,000,000.8989898989 

Use positive lookahead based regex...

content <- c("0 - 100", "0 - 100", "0 - 100", "0 - 100",
              "1000 - 2000","1000 - 2000", "1000 - 2000", "1000 - 2000", 
              "7000 - 10000", "7000 - 10000", "7000 - 10000", "7000 - 10000",
              "7000 - 10000", "1000000 - 22000000", "1000000 - 22000000", 
              "1000000 - 22000000")
gsub("(\\d)(?=(?:\\d{3})+\\b)", "\\1,", content, perl=T)
# [1] "0 - 100"                "0 - 100"                "0 - 100"               
# [4] "0 - 100"                "1,000 - 2,000"          "1,000 - 2,000"         
# [7] "1,000 - 2,000"          "1,000 - 2,000"          "7,000 - 10,000"        
# [10] "7,000 - 10,000"         "7,000 - 10,000"         "7,000 - 10,000"        
# [13] "7,000 - 10,000"         "1,000,000 - 22,000,000" "1,000,000 - 22,000,000"
# [16] "1,000,000 - 22,000,000"

Maybe you can use formatC :

sapply(
  X = lapply(
    X = strsplit(x = content, split = " - "),
    FUN = function(x) {
      formatC(x = as.numeric(x), format = "f", flag = "#", big.mark = ",", 
              decimal.mark = ".", digits = 2, drop0trailing = FALSE)
    }
  ),
  FUN = paste, collapse = " - "
)
# [1] "0.00 - 100.00"                 "0.00 - 100.00"                 "0.00 - 100.00"                
# [4] "0.00 - 100.00"                 "150.22 - 170.33"               "1,000.00 - 2,000.00"          
# [7] "1,000.00 - 2,000.00"           "1,000.00 - 2,000.00"           "1,000.00 - 2,000.00"          
# [10] "7,000.00 - 10,000.00"          "7,000.00 - 10,000.00"          "7,000.00 - 10,000.00"         
# [13] "7,000.00 - 10,000.00"          "7,000.00 - 10,000.00"          "1,000,000.00 - 22,000,000.00" 
# [16] "1,000,000.00 - 22,000,000.00"  "1,000,000.00 - 22,000,000.00"  "44,000,000.00 - 66,000,000.90"
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!