From 6b4a53b1a63593e9c7612253b46e5e63af95c834 Mon Sep 17 00:00:00 2001 From: Laura DeCicco <ldecicco@usgs.gov> Date: Thu, 28 Mar 2013 14:08:22 -0500 Subject: [PATCH] Added a short function, padVariable to help with problems of losing leading 0's. --- R/padVariable.r | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 R/padVariable.r diff --git a/R/padVariable.r b/R/padVariable.r new file mode 100644 index 00000000..de4f6500 --- /dev/null +++ b/R/padVariable.r @@ -0,0 +1,20 @@ +#' Pad string with leading zeros +#' +#' Function to pad a string with leading zeros. Useful for parameter codes and USGS site IDs. +#' +#' @param x string +#' @param padTo number Final desired length of the string +#' @keywords data import USGS web service +#' @return x string returned with leading zeros +#' @export +#' @examples +#' pCode <- '10' +#' correctPCode <- padVariable(pCode,5) +padVariable <- function(x,padTo){ + numDigits <- nchar(x) + if (padTo != numDigits){ + leadingZeros <- paste(rep("0",(padTo-numDigits)),collapse="",sep="") + x <- paste(leadingZeros,x,sep="") + } + return(x) +} \ No newline at end of file -- GitLab