Skip to content

Instantly share code, notes, and snippets.

@ramhiser
Created August 9, 2011 01:04
Show Gist options
  • Save ramhiser/1133190 to your computer and use it in GitHub Desktop.
Save ramhiser/1133190 to your computer and use it in GitHub Desktop.
Creates a zero-padded string from an integer.
# Creates a zero-padded string from an integer.
#
# The number of digits is defaulted to 3 but can be specified by the user.
# Examples:
# padded_integer(5) => "005"
# padded_integer(42) => "042"
# padded_integer(421) => "421"
# padded_integer(421, digits = 4) => "0421
#
padded_integer <- function(x, digits = 3) {
formatC(x, digits = digits - 1, flag = "0", format = "d")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment