Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use sprintf() to add trailing zeros

Tags:

r

There has to be a simple way of doing this and I am overlooking it. But if I have a series of id and want to add trailing zeros where the character limit is not reached. I saw this solution on another post but can't seem to find it to link for reference.

df$id <- c(2331,29623,311,29623)

Doing this gets the leading zero:

df$id_new <- sprintf("%05s", df$id)

But doing this does not get a trailing zero:

df$id_new <- sprintf("%-05s", df$id)

Answer

Thanks to Richard below, I pulled the stringr package and used the following to test:

df$id_test <- str_pad(df$id, width=5, side="right", pad="0")

Produced:

id_test  
23310  
29623  
31100  
29623
like image 247
Jebediah15 Avatar asked Nov 11 '15 17:11

Jebediah15


People also ask

How do you add trailing zeros to printf?

If "type" is 'e', 'E' or 'f', "printf" will always print out a decimal point (normally, the decimal point is omitted if the number has no fractional part). If "type" is '_f', trailing zeros are printed after a decimal point, even if the fractional part of the number is zero.

How do I keep the trailing zeros in Excel?

Set columns to text format. Click on a column to select it, then select an option to set the format for that column. Specifically, to keep leading and trailing zeros in columns containing phone numbers and similar data, select those columns and choose Text.

How do you add trailing zeros in Excel VBA?

Change the numbers from text to numbers (Convert To Number when you click on the exclamation mark next to the selected cells). Select and right-click the cells, select Format Cells , select Custom and enter 00000000 in the Type: box. The cell will contain the number 2, but display 00000002.


3 Answers

I don't think sprintf does that, but you can use formatC to do that

x <- 123    
formatC(as.numeric(x), format = 'f', flag='0', digits = 2)
[1] "123.00"
like image 177
romants Avatar answered Oct 23 '22 17:10

romants


As noted in the comments, sprintf() won't do this. But the stringi package has padding functions that are quick and easy.

id <- c(2331, 29623, 311, 29623)

library(stringi)
stri_pad_left(id, 5, 0)
# [1] "02331" "29623" "00311" "29623"
stri_pad_right(id, 5, 0)
# [1] "23310" "29623" "31100" "29623"

These functions dispatch directly to C code, so they should be sufficiently efficient.

like image 30
Rich Scriven Avatar answered Oct 23 '22 19:10

Rich Scriven


If we insist - we can fool sprintf() into doing what it doesn't want to do:

# Helper function from: stackoverflow.com/a/13613183/4552295
strReverse <- function(x) sapply(lapply(strsplit(x, NULL), rev), paste, collapse="")

so_0padr <- function(x, width = 5) {
  strReverse(
    sprintf(
      "%0*d",
      width,
      as.integer(
        strReverse(
          as.character(x)
        )
      )
    )
  )
}

Resulting in

so_0padr(c(2331,29623,311,29623))
[1] "23310" "29623" "31100" "29623"
like image 38
sindri_baldur Avatar answered Oct 23 '22 17:10

sindri_baldur