Printing a data frame with column alignment (as shown in R)

I have the following data frame in R:

> dframe Mean Median Candidates 85.68 60 NonCands 9.21 4 Multi 27.48 17 Mono 4.43 3 Multi NonCands 22.23 15 

I want to print it to a file and keep it beautifully formatted and aligned as shown above. I use:

 write.table(dframe,file="test",sep="\t", quote=F) 

which produces the following output:

 Mean Median Candidates 85.68 60 NonCands 9.21 4 Multi 27.48 17 Mono 4.43 3 Multi NonCands 22.23 15 

Since the data is displayed correctly formatted in the R environment, I thought it should be trivial to write to a file with the same format. Apparently, I was wrong. I tried playing with format() and write.matrix from the MASS library, but it does not give the desired result.

I saw several sentences, such as this one , but it seems too complicated and, more importantly, does not give the desired result when printing to a file with write.table() .

So, how can I print my data frame in a text file and look the same as in R?


UPDATE

Following Justin's suggestion from his comment below, I installed the gdata library and used write.fwf . This is almost what I need:

 write.fwf(dframe,file="test",sep="\t", quote=F, rownames=T) 

outputs the following result:

 Mean Median Candidates 85.68 60 NonCands 9.21 4 Multi 27.48 17 Mono 4.43 3 Multi NonCands 22.23 15 

So, any ideas on how to get β€œMean” and β€œMedian” have shifted to the right to fit their respective columns?

Since this might be relevant now, here's how to create a data.frame file:

 labels<-c("Candidates","NonCands","Multi", "Mono", "Multi NonCands") Mean <- c(mean(cands), mean(non),mean(multi),mean(mono),mean(multi_non)) Median <- c(median(cands), median(non),median(multi),median(mono),median(multi_non)) names(Mean)<-labels dframe<-data.frame(Mean,Median) 
+7
source share
6 answers

You can redirect print output to a file.

 max.print <- getOption('max.print') options(max.print=nrow(dframe) * ncol(dframe)) sink('dframe.txt') dframe sink() options(max.print=max.print) 
+6
source

You can also use capture.output with cat

 cat(capture.output(dframe), file = 'dframe.txt', sep = '\n') 
+6
source
 # Add the row names to the data frame, if you want them included dframe2 <- data.frame("Row"=rownames(dframe), dframe, stringsAsFactors=FALSE) # apply format over each column dframe2 <- apply(dframe2, 2, format) # print it out, make sure not to use quotes write.table(dframe2, "test.txt", quote=FALSE, row.names=FALSE) 
+1
source

I liked the simplicity of Matthew Proud's answer, but unfortunately it does not offer a way to get rid of the names / line numbers for my situation; so I changed Ricardo's answer a bit:

 print.to.file <- function(df, filename) { cnames <- colnames(df) n <- as.matrix(nchar(cnames)) d <- apply(df, 2, format) n <- apply(cbind(n, nchar(d[1,])), 1, max) fmts <- paste0("%",n, "s") for(i in 1:length(cnames)) { cnames[i] <- sprintf(fmts[i], cnames[i]) d[,i] <- sprintf(fmts[i], trimws(d[,i])) } d <- rbind(cnames, d) write.table(d, filename, quote=F, row.names=F, col.names=F) } 

This gives the same result from Matthew, with the exception of lines / row numbers.

EDIT: replace trim with trimws .

+1
source

Another way without line names based on the answer in mnel:

 prettyprint.df <- function(x, filename='dframe.txt', width=200) # Pretty-print data frame without row names { op.w <- options("width") options("width"=width) first <- max(nchar(rownames(x))) + 2 cat(substring(capture.output(x), first), file=filename, sep="\n") options(op.w) } 
+1
source

To control the output, configure print.data.frame to your needs and write the output to a file, for example:

 capture.output( print.data.frame(df, row.names=F, print.gap=3, quote=F, right=F), sep="\n", file="out.txt" ) 
+1
source

All Articles