diff --git a/R/padVariable.r b/R/padVariable.r new file mode 100644 index 0000000000000000000000000000000000000000..de4f65001ede6b7a959841419dc5c8d5d446561a --- /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