Skip to content
Snippets Groups Projects
Commit 7a50dee2 authored by Laura A DeCicco's avatar Laura A DeCicco
Browse files

Vectorized padVariable.

parent 27449d70
No related branches found
No related tags found
No related merge requests found
......@@ -10,11 +10,13 @@
#' @examples
#' pCode <- '10'
#' correctPCode <- padVariable(pCode,5)
#' pCodes <- c('100','1000','0','12345','1565465465465465')
#' correctPCodes <- padVariable(pCodes,5)
padVariable <- function(x,padTo){
if(padTo <= 1) return(x)
numDigits <- nchar(x)
if ((padTo-numDigits)>0){
leadingZeros <- paste(rep("0",(padTo-numDigits)),collapse="",sep="")
x <- paste(leadingZeros,x,sep="")
}
padding <- padTo-numDigits
padingZeros <- sapply(padding[padding > 0], function(y) paste(rep("0",y),collapse="",sep=""))
x[padding > 0] <- paste(padingZeros,x[padding > 0],sep="")
return(x)
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment