将 bin 标签格式化为货币

Posted

技术标签:

【中文标题】将 bin 标签格式化为货币【英文标题】:Format bin labels as currency 【发布时间】:2018-11-01 21:22:26 【问题描述】:

我想创建一些用于 ggplot 和 facet wrap 的 bin。我希望将它们格式化为货币,并希望防止它们以科学记数法显示。

来自 dput 的可重现数据:

x <- c(0, 1260.71, 2375.1, 1297.95, 0, 404.24, 0, 1027.03, 1034.3, 
    345.72, 1028.81, 257.52, 1088.32, 425.27, 75.85, 25.16, 0, 1759.73, 
    46.06, 4397.78, 3478.17, 456.78, 649.63, 576.64, 277.92, 220.79, 
    0, 3017.05, 0, 220.53, 722.27, 214.21, 557.1, 1082.09, 236.7, 
    0, 2265.52, 431.79, 1717.59, 874.65, 0, 1110.98, 191.04, 15.17, 
    1781.1, 624.32, 640.65, 11651.95, 0, 948.31, 172.11, 0, 359.64, 
    0, 99.39, 46.03, 1190.05, 0, 32.15, 191.52, 0, 1309.74, 24.16, 
    126.22, 0, 329.25, 0, 1676.46, 170.16, 24.74, 353.52, 0, 0, 389.18, 
    1597.22, 1810.97, 566.77, 425.85, 649.45, 3732.92, 0, 106.66, 
    24.17, 0, 752.91, 193.04, 0, 0, 176.84, 1251.59, 0, 0, 15.17, 
    48.51, 123.22, 335.52, 742.74, 606.43, 75.85, 19.17, 987.72, 
    230.2, 95.88, 1129.7, 616.45, 804.25, 786.24, 142.7, 15.17, 1723.59, 
    1132.47, 0, 297.68, 113.87, 984.85, 1705.17, 1511.1, 263, 0, 
    917.32, 84.85, 0, 335.56, 1440.97, 250.9, 91.81, 15.17, 204.18, 
    438.84, 602.61, 3313.78, 0, 602.51, 411.37, 608.1, 80.32, 540.79, 
    0, 272.47, 17.16, 0, 2520.45, 567.57, 85.84, 252.51, 20.17, 3463.09, 
    633.62, 12.17, 9745.27, 0, 150.8, 66.39, 1476.61, 0, 0, 150.22, 
    4465.15, 90.22, 12.18, 3850.22, 239.36, 141.96, 6568.14, 0, 1400.38, 
    0, 2001.17, 0, 247.34, 0, 50.14, 644.09, 0, 800.92, 0, 1295.01, 
    29.99, 1516.16, 184.25, 1170.77, 0, 462.15, 337.7, 359.88, 313.88, 
    196.75, 1514.12, 357.9, 732.92, 3.35, 240.67, 6025.2, 0, 0, 255.86, 
    1448.39, 203.76, 110.64, 168.02, 1490, 2086.08, 1640.99, 1231.65, 
    15.17, 0, 119.88, 30.34, 359.78, 138, 1927.33, 234.16, 57.28, 
    204.88, 493.19, 20.17, 1309.31, 129.39, 43.82, 200.26, 400.89, 
    0, 0, 0, 0, 883.38, 39.33, 0, 357.47, 165.27, 0, 575.01, 1178.9, 
    394.43, 1238.29, 349.04, 696.99, 758.49, 15.17, 1536.6, 1670.7, 
    26.96, 379.87, 0, 79.98, 114.22, 2753.45, 163.87, 1004.02, 371.94, 
    151.09, 280.38, 1984.17, 7253.37, 70.5, 255.46, 665.69, 179.88, 
    0, 505, 517.27, 85.84, 239.28, 0, 0, 758.14, 366.68, 1230.78, 
    281.99, 436.51, 338.12, 1000.62, 228.03, 232.38, 1131.07, 0, 
    0, 4874.65, 2019.69, 17.39, 710.51, 594.82, 166.02, 4261.5, 227.7, 
    328.81, 165.46, 87.83, 68.05, 1082.88, 72.68, 0, 120.25, 633.22, 
    119.88, 1328.22, 0, 146.2, 414.35, 125.87, 0, 170.93, 1376.79, 
    1161.3, 275.65, 489.57, 0, 250.44, 270.62, 259.87, 89.97, 4.99, 
    484.52, 231.39, 782.22, 0, 69.39, 173.87, 93.5, 1826.76, 1126.96, 
    15.17, 0, 0, 676.16, 25.16, 672.67, 422.93, 0, 712.48, 1172.13, 
    80.84, 600.38, 472.54, 704.63, 12.17, 0, 564.91, 922.04, 84.64, 
    43.15, 0, 4.99, 297.09, 1655.55, 611.81, 200.8, 392.74, 171.73, 
    147.05, 79.85, 931.68, 0, 105.33, 404.84, 153.23, 794.65, 471.03, 
    167.7, 167.93, 78.49, 1963.07, 222.56, 2171.89, 1643.24, 1078.19, 
    1198.09, 724.04, 0, 719.4, 34.58, 1307.71, 24.34, 641.03, 910.73, 
    0, 1246.63, 1122.6, 298.83, 1366.45, 488.12, 111.05, 7763.26, 
    112.04, 161.69, 627.38, 257.46, 0, 0, 85.56, 0, 0, 0, 2667.94, 
    1675.11, 299.4, 0, 373.92, 167.64, 15.17, 2119.01, 47.97, 0, 
    284.71, 343.74, 0, 463.32, 365.65, 400.91, 2534.48, 514.62, 0, 
    1437.5, 759.98, 314.6, 4071.02, 4498.42, 434.79, 164.93, 1099.35, 
    443.27, 77837.63, 361.11, 310.45, 2720.63, 5694.54, 1207.26, 
    69.61, 607.44, 39.98, 0, 507.72, 749.7, 20.99, 2020.75, 961.53, 
    124.19, 3483.86, 304.69, 89.48, 367.54, 13.99, 124.3, 25.16, 
    92.04, 538.08, 0, 3010.83, 24.36, 25.16, 601.97, 4.99, 616.27, 
    271.81, 0, 461.41, 150.69, 476.12, 305.19, 1742.41, 383.15, 399.73, 
    0, 95.88, 1713.9, 840.01, 175.05, 0, 584.9, 75.14, 15.17, 216.05, 
    0, 131.88, 837.31, 241.21, 176.02, 20.17, 1378.36, 1828.01, 0, 
    22376.54, 449.58, 435.93, 266.4, 8230.05, 34.34, 0, 268.1, 766.7, 
    0, 157.14, 1103.14, 75.85, 23078.46, 631.4, 210.7, 15.17, 360.56, 
    30.34, 112.48, 2765.84, 1021.47, 2231.56, 69.85, 0, 274.21, 1350.01, 
    1876.93, 1033.25, 510.16, 0, 391.61, 0, 215.38, 386.25, 40.33, 
    9119.57, 1171.74, 2709.87, 3071.29, 3558.81, 1406.53, 17.67, 
    113.07, 933.49, 394.49, 1298.92, 1096.99, 238.07, 128.52, 0, 
    1097.72, 1277.77, 0, 955.45, 430.26, 4545.52, 139.44, 0, 613.4, 
    119.88, 123.05, 1508.22, 190.75, 0, 0, 1609.56, 0, 535.66, 131.84, 
    2062.7, 3905.7, 0, 432.44, 1857.03, 219.05, 246.22, 0, 15.17, 
    403.01, 1216.77, 14.98, 5076.4, 642, 229.28, 7592.82, 0, 843.45, 
    59.99, 0, 4553.88, 630.15, 0, 0, 228.92, 0, 240.44, 466.03, 608.96, 
    257.51, 1387.26, 1194, 145.73, 246.07, 151.4, 0, 4313.8, 526.19, 
    649.25, 254.87, 232.67, 0, 419.59, 6105.63, 340.62, 0, 138.04, 
    23.34, 125.47, 420.36, 0, 3432.71, 19.96, 659.64, 150.21, 903.38, 
    0, 217.24, 6138.01, 0, 131.88, 0, 1373.48, 45.51, 403.58, 0, 
    0, 5316.52, 8892.27, 1022.93, 212.74, 534.42, 85.04, 29.98, 0, 
    1151.75, 47.76, 494.75, 269.25, 83.74, 0, 121.28, 322.08, 169.46, 
    1323.53, 60.68, 1079.12, 173.4, 155.85, 419.64, 332.94, 1119.05, 
    421.25, 2572.6, 95.88, 85.15, 0, 135.35, 15.17, 3849.37, 560.98, 
    1687.47, 19.17, 141.7, 0, 273.12, 92.03, 339.76, 187.11, 0, 90.16, 
    0, 0, 789.25, 0, 0, 236.54, 0, 2739.01, 917.26, 403.63, 267.33, 
    1238.8, 0, 0, 9836.92, 694.91, 57.97, 2468.21, 266.9, 274.61, 
    172.87, 1651.88, 0, 80.14, 1838.32, 402.94, 15.17, 1767.61, 0, 
    896.06, 1684.96, 3891.7, 0, 271.88, 1090.39, 195.8, 2357.32, 
    144.78, 6497.9, 489.89, 0, 235.47, 18.02, 4070.34, 1024.26, 17.88, 
    1364.22, 794.2, 5494.73, 609.39, 668.74, 66.32, 233.56, 0, 0, 
    1914.19, 559.52, 57, 135.05, 231.35, 621.38, 455.8, 566.27, 581.95, 
    612.33, 90.23, 23.16, 47.5, 7099.81, 8029.61, 180.39, 1038.75, 
    1527.79, 247.65, 0, 226.08, 0, 15.17, 2746.52, 483.67, 54.22, 
    71.29, 0, 790.49, 2858.83, 220.05, 2583.46, 79.03, 686.6, 0, 
    979.12, 84.26, 0, 30.34, 30.34, 85.04, 241.04, 254.2, 1229.18, 
    300.06, 252.87, 1184.37, 646.8, 0, 786.19, 442.91, 223.87, 2004.93, 
    375.05, 0, 654.84, 323.76, 349.2, 30.34, 503.91, 182.79, 367.26, 
    1405.29, 75.48, 3562.57, 1020.9, 675.94, 105.11, 0, 623.97, 58.99, 
    0, 289.43, 456.9, 20.17, 1807.69, 275.3, 114.16, 6999.62, 7251.8, 
    30.34, 0, 285.94, 382.33, 0, 0, 638.61, 921.95, 30.34, 144.04, 
    733.7, 20.17, 0, 50.33, 1085.81, 30.34, 366.93, 77.84, 1773.67, 
    809.41, 280.22, 740.83, 0, 0, 179.88, 487.8, 355.95, 550.45, 
    0, 179.88, 21560.55, 128.88, 471.72, 0, 5684.67, 11330.66, 159.05, 
    205.29, 637.6, 359.76, 0, 381.48, 383.92, 180.73, 172.03, 309.37, 
    806.85, 162.26, 639, 903.53, 614.75, 0, 23332.85, 483.04, 0, 
    190.17, 623.1, 299.68, 75.85, 128.18, 69.21, 177.04, 0, 3382.61, 
    100.15, 246.72, 142.04, 143.41, 332.69, 469.71, 409.74, 2255.62, 
    0, 645.28, 200.03, 75.85, 0, 59.88, 332.71, 275.52, 0, 317.99, 
    394.11, 150.69, 352.74, 623.97, 59.88, 2025.22, 2793.15, 150.04, 
    1676.92, 15.17, 4047.35, 365.26, 1827.32, 0, 334.26, 126.22, 
    247.18, 102.47, 266.34, 344.42, 3144.48, 102.84, 464.97, 0, 75.85, 
    30.34, 0, 1132.23, 99.84, 162.22, 0, 846.62, 167.97, 78.48, 377.65, 
    115.16, 347.66, 960.14, 404.91, 2338.54, 297.85, 0, 1041.79, 
    4751.19, 1316.62, 197.05, 0, 374.81, 131.36, 2433.18, 365.39, 
    154.67, 108.39, 262.17, 171.56, 436.75, 1488.71, 643.7, 24.53, 
    1083.99, 625.04, 1475.07, 2739.96, 2654.13, 301.86, 1317.23, 
    2190.86, 288.04, 1.17, 479.76, 75.85, 23.16, 147.05, 1552.83, 
    0, 88.79, 576.46, 0, 1204.46, 1988.73, 0, 178.93, 1443.55, 73.49, 
    448.9, 362.21, 551.48, 0, 740.24, 0, 15.17, 4856.03, 0, 363.06, 
    20.16, 111.05, 0, 3284.09, 96.37, 0, 179.12, 133.28, 276.5, 388.33, 
    1120.75, 19.99, 0, 0, 149.98, 136.53, 116.76, 773.21, 102.55, 
    169.17, 573.26, 261.76, 195.46, 102.32, 433.59, 0, 151.15)

这是垃圾箱:

xbin <- cut(x, breaks = c(0, 100, 1000, 10000))
> table(xbin)
xbin
      (0,100]   (100,1e+03] (1e+03,1e+04] 
          149           465           210

我希望 bin 标签是货币(美元)并且也以常规形式显示,而不是科学的,例如$0:100、$100:1,000 等

我该怎么做?

【问题讨论】:

cut 有标签参数。 您可以将这些中断分配为附加变量,并使类成为“因素”。那么您应该可以将该变量用作 ggplot 2 中的 x 变量。您也可以在 ggplot 中分配箱,但有时我也喜欢在我的数据框中使用因子变量。 相关帖子:***.com/questions/14456371/… 【参考方案1】:

使用标签

xbin <- cut(x, 
            breaks = c(0, 100, 1000, 10000),
            labels = c("$0:100", "$100:1,000", "$1000:10,000"))
table(xbin)
# $0:100   $100:1,000 $1000:10,000 
#    149          465          210 

或者使用 dig.lab 到avoid scientific notation:

xbin <- cut(x, 
            breaks = c(0, 100, 1000, 10000),
            dig.lab = 5)
table(xbin)
# xbin
# (0,100]   (100,1000] (1000,10000] 
#    149          465          210 

【讨论】:

【参考方案2】:

使用cutr,您可以定义自定义标签功能:

# devtools::install_github("moodymudskipper/cutr")
library(cutr)

xbin <- smart_cut(
  x, c(0, 100, 1000, 10000), expand = FALSE,
  labels = ~paste0("$",format(.y[1], big.mark=","),":", format(.y[2],big.mark=",")))
table(xbin)
# xbin
#       $0:100    $100:1,000 $1,000:10,000
#          318           465           210 

在标签函数内部,.y 包含两个边界,而.x 将包含它们之间的数据点。

您可以分开定义函数以便重用或提高可读性:

label_fun <- function(.x,.y) paste0("$",format(.y[1], big.mark=","),":", format(.y[2],big.mark=","))
xbin <- smart_cut(x, c(0, 100, 1000, 10000), expand = FALSE, labels = label_fun)
table(xbin)
# xbin
#       $0:100    $100:1,000 $1,000:10,000
#          318           465           210 

【讨论】:

以上是关于将 bin 标签格式化为货币的主要内容,如果未能解决你的问题,请参考以下文章

如何仅使用货币代码将数字格式化为货币?

如何将十进制属性格式化为货币?

将数字格式化为货币金额

如何将数字格式化为货币字符串

如何使用正则表达式将数字格式化为货币

将数字数组格式化为具有最小宽度的货币