This commit is contained in:
Andreas Gammelgaard Damsbo 2024-02-07 20:56:05 +01:00
parent 0cde2918ee
commit 8133684f54
2 changed files with 22 additions and 6 deletions

View File

@ -4,13 +4,15 @@
#' @param length final string length
#' @param after if padding should be added after as opposed to default before
#' @param pad padding string of length 1
#' @param lead leading string for all. Number or character vector. Cycled.
#' @param tail tailing string for all. Number or character vector. Cycled.
#'
#' @return vector or character strings of same length.
#' @export
#'
#' @examples
#' add_padding(sample(1:200,5))
add_padding <- function(d,length=NULL,after=FALSE,pad="0"){
#' add_padding(sample(1:200,5),tail="AA",lead=c(2,3,"e"))
add_padding <- function(d,length=NULL,after=FALSE,pad="0",lead=NULL,tail=NULL){
if (!is.vector(d)) {
stop("Please supply vector")
}
@ -31,8 +33,11 @@ add_padding <- function(d,length=NULL,after=FALSE,pad="0"){
paste(rep(pad,i),collapse="")}))
if (after) {
paste0(d,ps)
out <- paste0(d,ps)
} else {
paste0(ps,d)
out <- paste0(ps,d)
}
paste0(lead,out,tail)
}

View File

@ -4,7 +4,14 @@
\alias{add_padding}
\title{Add padding to string}
\usage{
add_padding(d, length = NULL, after = FALSE, pad = "0")
add_padding(
d,
length = NULL,
after = FALSE,
pad = "0",
lead = NULL,
tail = NULL
)
}
\arguments{
\item{d}{vector of strings or numbers}
@ -14,6 +21,10 @@ add_padding(d, length = NULL, after = FALSE, pad = "0")
\item{after}{if padding should be added after as opposed to default before}
\item{pad}{padding string of length 1}
\item{lead}{leading string for all. Number or character vector. Cycled.}
\item{tail}{tailing string for all. Number or character vector. Cycled.}
}
\value{
vector or character strings of same length.
@ -22,5 +33,5 @@ vector or character strings of same length.
Add padding to string
}
\examples{
add_padding(sample(1:200,5))
add_padding(sample(1:200,5),tail="AA",lead=c(2,3,"e"))
}