just throw a copy of this into your project path in the utils directory and source it when you run the script
"formatColumns" <- function(data, digits) { "%,%" <- function(x,y)paste(x,y,sep="") nms <- names(data) nc <- ncol(data) nd <- length(digits) if(nc!=nd) stop("Argument 'digits' must be vector of length " %,% nc %,% ", the number of columns in 'data'.") out <- as.data.frame(sapply(1:nc, FUN=function(x, d, Y) format(Y[,x], digits=d[x]), Y=tbl, d=digits)) if(!is.null(nms)) names(out) <- nms out }
Now you can relax and unwind.
formatColumns(MyData, digits=c(0,2,4,4,4,0,0))
et cetera et cetera et cetera
user41990
source share