gpt4 book ai didi

r 用 0 和破折号格式化数字

转载 作者:行者123 更新时间:2023-12-02 05:40:25 25 4
gpt4 key购买 nike

我正在处理下面这样的数字

   12344
233345
7233433
9343883
3432837

我正在尝试对这些数字进行格式化,使它们的长度都相同,每三个数字后都有短划线 (-)。最终的预期结果应该如下所示。

  0-012-344
0-233-345
7-233-433
9-343-883
3-432-837

非常感谢以这种方式格式化这些数字的任何帮助。

最佳答案

尝试formatC:

formatC(x, width = 7, format = "d", flag = "0", big.mark = '-')
# [1] "0-012-344" "0-233-345" "7-233-433" "9-343-883" "3-432-837"

使用的数据:

x <- scan(textConnection('12344
233345
7233433
9343883
3432837'))

关于r 用 0 和破折号格式化数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46149046/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com