如何添加前导零?

baz*_*baz 320 formatting r number-formatting r-faq

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

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
Run Code Online (Sandbox Code Playgroud)

我希望在每个动物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
Run Code Online (Sandbox Code Playgroud)

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

Ric*_*ton 504

简短版本:使用formatCsprintf.


版本较长:

有几种功能可用于格式化数字,包括添加前导零.哪一个最好取决于您想要做的其他格式.

问题的例子非常简单,因为所有的值都有相同的数字位数,所以让我们尝试一个更难的例子来制作10宽度8的幂.

anim <- 25499:25504
x <- 10 ^ (0:5)
Run Code Online (Sandbox Code Playgroud)

paste(而且它的变体paste0)通常是您遇到的第一个字符串操作函数.它们并非真正用于操纵数字,但它们可用于此.在我们总是必须预先设置单个零的简单情况下,这paste0是最好的解决方案.

paste0("0", anim)
## [1] "025499" "025500" "025501" "025502" "025503" "025504"
Run Code Online (Sandbox Code Playgroud)

对于数字中存在可变位数的情况,您必须手动计算要预先设置的零数,这是非常可怕的,您应该只是出于病态的好奇心.


str_padstringr类似的工作paste,使你更明确,你想要填充东西.

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

同样,它并不是真正设计用于数字,所以更难的情况需要一点思考.我们应该只能说"用零填充到宽度为8",但看看这个输出:

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

您需要设置科学惩罚选项,以便始终使用固定符号(而不是科学符号)格式化数字.

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

stri_padstringi工作中完全像str_padstringr.


formatC是C函数的接口printf.使用它需要了解该底层函数的arcana(参见链接).在这种情况下,重要的点是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"
Run Code Online (Sandbox Code Playgroud)

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


sprintf是同名C函数的接口; 喜欢formatC但语法不同.

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

主要优点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." 
Run Code Online (Sandbox Code Playgroud)

另见goodside的答案.


为了完整性,值得一提的是偶尔有用的其他格式化函数,但没有预先填充零的方法.

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

prettyNum是另一种格式化功能,主要用于创建手动轴刻度标签.它适用于各种数字.

scales包装具有多种功能,如percent,date_formatdollar专科格式类型.

  • 非常感谢你的帮助.我使用formatC为我的动画添加前导零,它运行良好. (2认同)
  • formatC(数字或向量,宽度= 6,格式="d",标志="0")效果很好(R版本3.0.2(2013-09-25)).谢谢. (2认同)
  • @ user1816679听起来你忘了`flag ="0"`. (2认同)

goo*_*ide 206

对于无论有多少位数都能正常工作的一般解决方案data$anim,请使用该sprintf功能.它的工作原理如下:

sprintf("%04d", 1)
# [1] "0001"
sprintf("%04d", 104)
# [1] "0104"
sprintf("%010d", 104)
# [1] "0000000104"
Run Code Online (Sandbox Code Playgroud)

在您的情况下,您可能想要: data$anim <- sprintf("%06d", data$anim)

  • 请注意,`sprintf`将数字转换为字符串(字符). (14认同)

met*_*oia 30

扩展@ goodside的回复:

在某些情况下,您可能希望用零填充字符串(例如,fips代码或其他类似数字的因子).在OSX/Linux中:

> sprintf("%05s", "104")
[1] "00104"
Run Code Online (Sandbox Code Playgroud)

但是因为在Windows 7中sprintf()调用操作系统的C sprintf()命令(在此处讨论),您会得到不同的结果:

> sprintf("%05s", "104")
[1] "  104"
Run Code Online (Sandbox Code Playgroud)

所以在Windows机器上的解决方法是:

> sprintf("%05d", as.numeric("104"))
[1] "00104"
Run Code Online (Sandbox Code Playgroud)


kda*_*ria 24

str_padstringr包中可以选择.

anim = 25499:25504
str_pad(anim, width=6, pad="0")
Run Code Online (Sandbox Code Playgroud)

  • 小心使用“ str_pad”会导致意外结果。```i.num = 600000`''; ```str_pad(i.num,width = 7,pad =“ 0”)```会给你“ 006e + 05”而不是“ 0600000” (2认同)