本文介绍了如何添加前导零?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a set of data which looks something like this:

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之前添加零:

I would like a zero to be added before each animal 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之前添加两个或三个零会怎样?

And for interest sake, what if I need to add two or three zeros before the animal id's?

推荐答案

简短版本:使用 formatC sprintf .

The short version: use formatC or sprintf.

较长版本:

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

There are several functions available for formatting numbers, including adding leading zeroes. Which one is best depends upon what other formatting you want to do.

这个问题的示例非常容易,因为所有值的开头都是相同的数字,所以让我们尝试一个更难的示例,即也将10的宽度乘以8的幂.

The example from the question is quite easy since all the values have the same number of digits to begin with, so let's try a harder example of making powers of 10 width 8 too.

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


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


paste (and it's variant paste0) are often the first string manipulation functions that you come across. They aren't really designed for manipulating numbers, but they can be used for that. In the simple case where we always have to prepend a single zero, paste0 is the best solution.

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

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

For the case where there are a variable number of digits in the numbers, you have to manually calculate how many zeroes to prepend, which is horrible enough that you should only do it out of morbid curiosity.

str_pad paste的工作方式相似,使您更清楚地表示要填充内容.

str_pad from stringr works similarly to paste, making it more explicit that you want to pad things.

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

同样,它并不是真正为数字而设计的,因此更难的情况需要考虑一下.我们应该只能说零填充到宽度8",但请看下面的输出:

Again, it isn't really designed for use with numbers, so the harder case requires a little thinking about. We ought to just be able to say "pad with zeroes to width 8", but look at this output:

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

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

You need to set the scientific penalty option so that numbers are always formatted using fixed notation (rather than scientific notation).

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


中的 stri_pad c9>的工作方式与stringr中的str_pad完全一样.


stri_pad in stringi works exactly like str_pad from stringr.

formatC 是一个接口C函数 printf .使用它需要一些有关该基本功能的奥秘的知识(请参阅链接).在这种情况下,要点是width自变量,format"d"表示整数",而"0" flag是前置零.

formatC is an interface to the C function printf. Using it requires some knowledge of the arcana of that underlying function (see link). In this case, the important points are the width argument, format being "d" for "integer", and a "0" flag for prepending zeroes.

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"

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

This is my favourite solution, since it is easy to tinker with changing the width, and the function is powerful enough to make other formatting changes.

sprintf 是一个接口同名的C函数;类似于formatC,但语法不同.

sprintf is an interface to the C function of the same name; like formatC but with a different syntax.

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

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

The main advantage of sprintf is that you can embed formatted numbers inside longer bits of text.

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."

另请参见好端的答案.

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

For completeness it is worth mentioning the other formatting functions that are occasionally useful, but have no method of prepending zeroes.

format ,一个通用功能用于格式化任何类型的对象,并带有用于数字的方法.它的作用有点像formatC,但具有另一个接口.

format, a generic function for formatting any kind of object, with a method for numbers. It works a little bit like formatC, but with yet another interface.

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

prettyNum is yet another formatting function, mostly for creating manual axis tick labels. It works particularly well for wide ranges of numbers.

scales 软件包具有几个功能,例如 percent date_format dollar 用于专业格式类型.

The scales package has several functions such as percent, date_format and dollar for specialist format types.

这篇关于如何添加前导零?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 02:47