r - 如何添加前导零?

我有一组看起来像这样的数据:

anim <- c(25499,25500,25501,25502,25503,25504)
sex  <- c(1,2,2,1,2,1)
wt   <- c(0.8,1.2,1.0,2.0,1.8,1.4)
data <- data.frame(anim,sex,wt)

data
   anim sex  wt anim2
1 25499   1 0.8     2
2 25500   2 1.2     2
3 25501   2 1.0     2
4 25502   1 2.0     2
5 25503   2 1.8     2
6 25504   1 1.4     2

我想在每个动物 id 之前添加一个零:

data
   anim sex  wt anim2
1 025499   1 0.8     2
2 025500   2 1.2     2
3 025501   2 1.0     2
4 025502   1 2.0     2
5 025503   2 1.8     2
6 025504   1 1.4     2

为了兴趣,如果我需要在动物 id 之前添加两个或三个零怎么办?

最佳答案

短版:使用 formatCsprintf .


更长的版本:

有多种函数可用于格式化数字,包括添加前导零。哪个最好取决于您要执行的其他格式。

问题中的示例非常简单,因为所有值的开头位数都相同,所以让我们尝试一个更难的示例,将 10 的幂也设为 8。

anim <- 25499:25504
x <- 10 ^ (0:5)

paste (它的变体 paste0)通常是您遇到的第一个字符串操作函数。它们并不是真正为操纵数字而设计的,但它们可以用于此目的。在我们总是必须在前面加上一个零的简单情况下,paste0 是最好的解决方案。

paste0("0", anim)
## [1] "025499" "025500" "025501" "025502" "025503" "025504"

对于数字中位数可变的情况,您必须手动计算要添加多少个零,这太可怕了,您应该出于病态的好奇心才这样做。


str_pad stringr 中的 paste 的工作方式类似,使您想要填充的内容更加明确。

library(stringr)
str_pad(anim, 6, pad = "0")
## [1] "025499" "025500" "025501" "025502" "025503" "025504"

同样,它并不是真正为数字设计而设计的,因此更难的情况需要稍微考虑一下。我们应该只能说“用零填充到宽度 8”,但看看这个输出:

str_pad(x, 8, pad = "0")
## [1] "00000001" "00000010" "00000100" "00001000" "00010000" "0001e+05"

您需要设置科学惩罚option所以数字总是使用固定记数法(而不是科学记数法)格式化。

library(withr)
with_options(
  c(scipen = 999), 
  str_pad(x, 8, pad = "0")
)
## [1] "00000001" "00000010" "00000100" "00001000" "00010000" "00100000"

stri_pad stringi 中的 stringr 中的 str_pad 完全相同。


formatC 是 C 函数的接口(interface) printf .使用它需要对该底层功能的奥秘有一些了解(见链接)。在这种情况下,重点是 width 参数,format"d" 表示“整数”,以及 "0 " flag 用于添加零。

formatC(anim, width = 6, format = "d", flag = "0")
## [1] "025499" "025500" "025501" "025502" "025503" "025504"
formatC(x, width = 8, format = "d", flag = "0")
## [1] "00000001" "00000010" "00000100" "00001000" "00010000" "00100000"

这是我最喜欢的解决方案,因为它很容易修改宽度,并且功能强大到足以进行其他格式更改。


sprintf 是同名 C 函数的接口(interface);类似于 formatC 但语法不同。

sprintf("%06d", anim)
## [1] "025499" "025500" "025501" "025502" "025503" "025504"
sprintf("%08d", x)
## [1] "00000001" "00000010" "00000100" "00001000" "00010000" "00100000"

sprintf 的主要优点是您可以在较长的文本中嵌入格式化数字。

sprintf(
  "Animal ID %06d was a %s.", 
  anim, 
  sample(c("lion", "tiger"), length(anim), replace = TRUE)
)
## [1] "Animal ID 025499 was a tiger." "Animal ID 025500 was a tiger."
## [3] "Animal ID 025501 was a lion."  "Animal ID 025502 was a tiger."
## [5] "Animal ID 025503 was a tiger." "Animal ID 025504 was a lion." 

另见 goodside's answer .


为了完整起见,值得一提的是其他偶尔有用的格式化函数,但没有添加零的方法。

format ,用于格式化任何类型的对象的通用函数,具有数字方法。它的工作方式有点像 formatC,但有另一个界面。

prettyNum 是另一个格式化函数,主要用于创建手动轴刻度标签。它特别适用于大范围的数字。

scales 包有几个函数,例如 percent , date_formatdollar用于专业格式类型。

https://stackoverflow.com/questions/5812493/

相关文章:

python - 如何防止 Python 打印添加换行符或空格?

objective-c - 使 float 只显示两位小数

r - 如何禁用科学记数法?

git - 如何仅使用第一行输出 git log?

r - 格式化R中的小数位

formatting - Intellij 重新格式化文件保存

javascript - 将 JavaScript 日期格式化为 yyyy-mm-dd

sql-server - 在 SQL Server Management Studio 中格式化 S

android - 如何在 Android 中格式化日期和时间?

formatting - Sublime Text 3 中的 80 个字符/右边距线