如何通过在R中添加零来使用于观察的不同长度代码相等?

时间:2021-08-14 12:32:22

I have observations on different families as given by variable folio:

我对变量作品集给出的不同家庭有观察结果:

Folio

1. 10
2. 10
3. 30
4. 30
.
.
.
828. 7110
829. 7130
.
.
.
11841. 107570
11842. 107580
11843. 107590

However, as I have other databases with a different format, I need to add zeros to be able to have six digit codes for all observations so they look this way:

但是,由于我有其他数据库具有不同的格式,我需要添加零,以便能够为所有观察设置六位数代码,因此它们看起来像这样:

1. 000010
2. 000010
3. 000030
4. 000030
.
.
.
828. 007110
829. 007130
.
.
.
11841. 107570
11842. 107580
11843. 107590

Any advice?

2 个解决方案

#1


1  

This works: Folio <- sprintf("%06d",Folio)

这有效:Folio < - sprintf(“%06d”,Folio)

#2


1  

Using the awesome library stringr:

使用awesome库stringr:

library(stringr)
str_pad(folio, width = 6, side = c("left"), pad = "0")

#1


1  

This works: Folio <- sprintf("%06d",Folio)

这有效:Folio < - sprintf(“%06d”,Folio)

#2


1  

Using the awesome library stringr:

使用awesome库stringr:

library(stringr)
str_pad(folio, width = 6, side = c("left"), pad = "0")