Skip to content
Prev 367012 / 398506 Next

Beginner needs help with R

I think it is important to point out that whenever R treats a number as a numeric (integer or double) it loses any base 10 concept of "leading zero" in that internal representation, so in this expression

seq2 <- paste0("DQ", sprintf("%06d", seq(060054, 060060)))

the arguments to seq have leading zeros that are ignored by R and have nothing to do with getting the desired output. That is,  the same result can be obtained using

seq2 <- paste0("DQ", sprintf("%06d", seq(60054, 60060)))

or

seq2 <- paste0("DQ", sprintf("%06d", seq(0060054, 00060060)))

since only the zero inside the format string is key to success. (If it makes you more comfortable to put the zero there for readability that is your choice, but R ignores therm.)

Also note that the paste0 function is not needed when you use sprintf:

seq2 <- sprintf("DQ%06d", seq(60054, 60060))

or

myprefix <- "DQ"
seq2 <- sprintf("%s%06d", myprefix,seq(60054, 60060))