使用R添加前导零 [英] Adding leading zeros using 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
感兴趣的缘故,如果我需要在动物身份证之前添加两三个零呢? 简短的版本:use
formatC
或sprintf
。
更长的版本:
格式化数字有几个功能,包括添加前导零。哪一个是最好的取决于你想要做的其他格式。
这个问题的例子很容易,因为所有的值都有相同的位数,所以我们来尝试一个比较宽泛的例子:
anim< - 25499:25504
x < - 10 ^(0:5)
粘贴
(它的变体paste0
)通常是你遇到的第一个字符串操作函数。它们并不是真正用于操纵数字的,但是可以用于这个目的。在这种简单的情况下,我们总是需要预先设置一个零,paste0
是最好的解决方案。
paste0(0,anim)
## [1]025499025500025501025502025503025504
$ b对于数字中可变数字位数的情况,您必须手动计算需要多少个零,这太可怕了你应该只是出于病态的好奇心才行。
str_pad
来自stringr
与paste
的工作方式类似,使得它更加明确地表明你想要做的事情。library(stringr)
str_pad(anim,6,pad =0)
## [1]025499025500025501025502 025503025504
同样,它不是真正用于数字的,所以更难的情况下需要一点思考。
str_pad(x)这个输出可以是零到宽8 ,8,pad =0)
/ pre>
## [1]00000001000000100000010000001000000100000001e + 05
您需要设定科学罚款选项,这样数字总是使用固定的符号(而不是科学记数法)格式化。
library( withr)
with_options(
c(scipen = 999),
str_pad(x,8,pad =0)
)
## [1]00000001 0000001000000100000010000001000000100000
stri_pad $
。stringi
的工作原理与str_pad
完全一样。 > stringr
formatC
是C函数的一个接口。printf
。使用它需要一些关于这个潜在功能的神秘的知识(见链接)。在这种情况下,重要的是宽度
参数,格式是
d
代表整数,而0
标记
用于预置零。 >
formatC(anim,width = 6,format =d,flag =0)
## [1] 025499025500025501025502025503025504
formatC(x,width = 8,format =d,flag =0)
## [1 ]000000010000001000000100000010000001000000100000
这是我最喜欢的解决方案,因为它很容易修改宽度,而且功能足够强大,可以进行其他格式更改。
sprintf
是同名的C函数的接口;例如formatC
,但使用不同的语法。sprintf(%06d,anim)
## [1]025499025500025501 025502025503025504
sprintf(%08d,x)
## [1]000000010000001000000100000010000001000000100000
sprintf 的主要优点在于你可以将格式化的数字嵌入较长的文本内。 sprintf(
动物ID%06d是%s。 ,
anim,
sample(c(lion,tiger),length(anim),replace = TRUE)
)
## [1] ID 025499是一只老虎。 动物ID 025500是一只老虎。
## [3]动物ID 025501是狮子。 动物ID 025502是一只老虎。
## [5]动物ID 025503是一只老虎。 动物ID 025504是一只狮子。
另见 goodside's answer 。
为了完整起见,值得一提的是其他格式化函数偶尔有用,但是没有预先置零的方法。
format
,一种通用函数,用于格式化任何类型的对象,并使用数字方法。它有点像formatC
,但有另一个接口。
prettyNum
是另一种格式功能,主要用于创建手动轴刻度标签。它适用于广泛的数字。
scale 包有几个功能,例如 percent
,date_format
和<对于专业格式类型,dollar
。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
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
And for interest sake, what if I need to add two or three zeros before the animal id's?
解决方案The short version: use
formatC
orsprintf
.
The longer version:
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.
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
(and it's variantpaste0
) 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
fromstringr
works similarly topaste
, 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"
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
instringi
works exactly likestr_pad
fromstringr
.
formatC
is an interface to the C functionprintf
. Using it requires some knowledge of the arcana of that underlying function (see link). In this case, the important points are thewidth
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
is an interface to the C function of the same name; likeformatC
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"
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."
See also goodside's answer.
For completeness it is worth mentioning the other formatting functions that are occasionally useful, but have no method of prepending zeroes.
format
, a generic function for formatting any kind of object, with a method for numbers. It works a little bit likeformatC
, but with yet another interface.
prettyNum
is yet another formatting function, mostly for creating manual axis tick labels. It works particularly well for wide ranges of numbers.The
scales
package has several functions such aspercent
,date_format
anddollar
for specialist format types.这篇关于使用R添加前导零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!