R/align.around.decimal.point.r

Defines functions align.around.decimal.point

Documented in align.around.decimal.point

#' Pad Numeric Vector Around Decimal Point
#' 
#' Pads a vector of numeric values so that the decimal is at the same ordinal location.
#' @param x A vector of numbers.
#' @param sep The decimal separator.
#' @param len The total character width desired in the formatted field.
#' @return A character vector with members of length \code{len}, with \code{sep} located in the same position
#' of each member.
#' @export align.around.decimal.point
#' @usage align.around.decimal.point(x, sep = '\\\\.', len)
#' @examples
#' align.around.decimal.point(c(1,100, 0.01))

align.around.decimal.point  =
  function(x, sep = "\\.", len)
  {
    xx = unlist(lapply(lapply(paste(x), unPaste, sep = sep), function(x)
      x[[1]]))
    if (missing(len))
      len = max(nchar(xx))
    sapply(seq(along = x), function(y, x, yy, len)
      paste(paste(
        "$\\phantom{",
        paste(rep("0", (len - nchar(
          yy[y]
        ))), collapse = ""), "}$", sep = ""
      ),
      x[y], sep = ""), yy = xx, x = x, len = len)
  }

# create short-hand alias
aadp = align.around.decimal.point
qPharmetra/qpToolkit documentation built on May 24, 2023, 8:52 a.m.