R/buffer_matrix.R

Defines functions buffer_matrix

# This function takes a character matrix and "buffers" each entry with blank
# spaces so each column's entries have the same length, equal to the length of
# the longest string in that column.
buffer_matrix <- function(mat){
  stopifnot(is.matrix(mat), is.character(mat))
  min_width <- apply(mat, MARGIN = 2, FUN = function(x) max(nchar(x)))
  output <- vector(mode = "list", length = 4)
  for(i in seq_len(ncol(mat))){
      mat[,i] <- sprintf(paste0("%", min_width[i], "s"), mat[,i])
  }
  return(mat)
}

Try the distfreereg package in your browser

Any scripts or data that you put into this service are public.

distfreereg documentation built on April 4, 2025, 12:30 a.m.