You can use :
or seq
for sequences and you can prepend your leading text with paste
or paste0
. The heart of the question is on the number padding with leading 0's.
Your options are:
stri_pad
from stringi
(more intuitive)
str_pad
from stringr
(more intuitive)
sprintf
(no packages needed)
formatC
(good if you're familiar with C's printf
)
Note that some cases, though not this particular one, necessitate disabling scientific notation for numbers in the sequence. This can be done with options
or with_options
from devtools
.
Please see this popular post for examples of each.
Using formatC
:
uid <- paste0("AKM_CC_Test_", formatC(1:10000, width = 18, format = "d", flag = "0"))
head(uid)
[1] "AKM_CC_Test_000000000000000001" "AKM_CC_Test_000000000000000002" "AKM_CC_Test_000000000000000003" "AKM_CC_Test_000000000000000004"
[5] "AKM_CC_Test_000000000000000005" "AKM_CC_Test_000000000000000006"
Using the stringr
package:
uid <- paste0("AKM_CC_Test_", str_pad(1:10000, 18, pad = "0")) # remember to load stringr
head(uid)
[1] "AKM_CC_Test_000000000000000001" "AKM_CC_Test_000000000000000002" "AKM_CC_Test_000000000000000003" "AKM_CC_Test_000000000000000004"
[5] "AKM_CC_Test_000000000000000005" "AKM_CC_Test_000000000000000006"
Using sprintf
:
head(sprintf("%s%0*d", "AKM_CC_Test_", 18, 1:10000))
[1] "AKM_CC_Test_000000000000000001" "AKM_CC_Test_000000000000000002" "AKM_CC_Test_000000000000000003" "AKM_CC_Test_000000000000000004"
[5] "AKM_CC_Test_000000000000000005" "AKM_CC_Test_000000000000000006"
Using stri_pad
from the package stringi
:
uid <- paste0("AKM_CC_Test_", stri_pad(1:10000, 18, pad = "0")) # remember to load stringi
head(uid)
[1] "AKM_CC_Test_000000000000000001" "AKM_CC_Test_000000000000000002" "AKM_CC_Test_000000000000000003" "AKM_CC_Test_000000000000000004"
[5] "AKM_CC_Test_000000000000000005" "AKM_CC_Test_000000000000000006"