14

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 into a file and keep it nicely formatted and aligned just 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 properly formatted within the R environment I thought it should be trivial to write it to a file with the same format. Apparently I was wrong. I have tried playing with format() and write.matrix from the MASS library but neither produces the desired result.

I have seen some suggestions like this one, but it seems both too complicated and, more importantly, does not produce the desired result when printing to a file with write.table().

So, how can I print my data frame to a text file and have it look just as it does within 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)

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

So, any ideas on how to get "Mean" and "Median" shifted to the right so they align with their respective columns?

Since it may now be relevant, here is how the data.frame was created:

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)
Community
  • 1
  • 1
terdon
  • 3,260
  • 5
  • 33
  • 57
  • 3
    take a look at `write.fwf` from the `gdata` package. `fwf` stands for fixed width file. however, if the font used to view a text file isn't fixed width, it might not be even. – Justin Nov 27 '12 at 18:42
  • To be precise, you want the rownames left-aligned and padded, and the columns right-aligned. (`print.data.frame(... right=X)` only allows that option to be set globally, so a little tweaking needed). – smci Jun 25 '15 at 22:23

6 Answers6

9

You could also use capture.output with cat

cat(capture.output(dframe), file = 'dframe.txt', sep = '\n')
mnel
  • 113,303
  • 27
  • 265
  • 254
6

You could redirect the output of print to file.

max.print <- getOption('max.print')
options(max.print=nrow(dframe) * ncol(dframe))
sink('dframe.txt')
dframe
sink()
options(max.print=max.print)
Matthew Plourde
  • 43,932
  • 7
  • 96
  • 113
  • 2
    You may experience `getOption("max.print")` issue if the number of rows are greater than `max.print` value. – Sezen May 30 '13 at 20:45
  • @MatthewPlourde, please see my answer below, is there a way to get rid of row names/numbers in your simple solution? – Sezen May 31 '13 at 06:00
  • 1
    @Sezen You could convert the data.frame to a matrix and set them all to the empty character. – Matthew Plourde May 31 '13 at 08:57
3

To control the output, tune print.data.frame to your needs and capture the output to a file, eg.:

capture.output(
  print.data.frame(df, row.names=F, print.gap=3, quote=F, right=F),
  sep="\n", file="out.txt"
)
jnas
  • 826
  • 10
  • 14
2
# 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)
Ricardo Saporta
  • 54,400
  • 17
  • 144
  • 178
1

I liked the simplicity the answer of Matthew Plourde, but unfortunately, it does not offer a way to get rid of the row names/numbers for my situation; so, I modified a little bit the answer of Ricardo:

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 output of Matthew's except row names/numbers.

EDIT: replace trim with trimws.

Sezen
  • 447
  • 1
  • 5
  • 17
  • 1
    Unfortunately in using the latest R... `Error in trim(d[, i]) : could not find function "trim"` – AF7 Nov 17 '17 at 11:12
1

Another way without row names based on the answer of 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)  
}  
Community
  • 1
  • 1
jpjns
  • 11
  • 1