0

I want to create a function that pads zeros no the left until the number is exact 5-digit. For that I’ve created the following function called prefix.

prefix <- function(baa) {

        for (i in 1:(5 - nchar(toString(baa)))) {
                    baa <- paste0("0", baa)
        }
        baa
}

This function works well with a single integer, but fails to do its job if it receives a vector. My goal is to create a function that will return

00001
00012
00123
01234
12345

given

1
12
123
1234
12345

I think this could be easely achieved with lapply, but I havent figuered it out yet. Can you please help?

Eduardo
  • 2,405
  • 3
  • 14
  • 19

0 Answers0