-2

I am running for loop. I have three for loops in one chunk.

for(k in 01:12){
  for (j in 01:31) {
    for (i in 00:23) {
      file = paste0("2017-", k ,"-", j ," ", i, "_00_00/Registrations.csv") 

In this for loop i, j and kare taking values like 0,1,2,3,4,5,6,7,8,9,10,11,... so on

I need to get these i, j and k like 00,01,02,03,04,05,06,07,08,09,10,11,12... and so on

How do I format these i,j and k to get desired 2 digit values?? Thanks in advance.

Bea
  • 1,110
  • 12
  • 20
P.Nitu
  • 1
  • 1
  • 5

1 Answers1

2

Instead of a loop, we can create the combination of values as a data.frame and then use sprintf to get the format

res <- do.call(sprintf, c(expand.grid(1:12, 1:31, 0:23),
             fmt = "2017-%02d-%02d %02d_00_00/Registration.csv"))
head(res)
#[1] "2017-01-01 00_00_00/Registration.csv" "2017-02-01 00_00_00/Registration.csv"
#[3] "2017-03-01 00_00_00/Registration.csv" "2017-04-01 00_00_00/Registration.csv"
#[5] "2017-05-01 00_00_00/Registration.csv" "2017-06-01 00_00_00/Registration.csv"

If the OP really needed the loop, just change the paste0 with sprintf

for(k in 01:12){
 for (j in 01:31) {
   for (i in 00:23) {
    file <- sprintf("2017-%02d-%02d %02d_00_00/Registration.csv", k, j, i)  
     --
    --
  }
  }
 } 
akrun
  • 874,273
  • 37
  • 540
  • 662