Today I had to pretty-print a summary of a R data-frame composed of lots of factors. I thought I would use LaTeX and the xtable package.
Unfortunately, if you try the natural
xtable(summary(data))
you get a table that is very, very wide. Since there’s no easy way in LaTeX to split wide tables over multiple pages, that approach won’t work.
Here, instead is my trick for pretty-printing such summaries (basically shoving everything into a matrix and using xtable on that). This won’t work if you have a period in any of your variable names. Kludgy, I know.
foo<-sapply(data,summary) ## get summary of each var.
bar<-matrix(nrow=length(unlist(foo)),ncol=3)bar[,1]<-gsub(”\\..*”,”",names(unlist(foo))) ## first column has variable names
bar[,1][duplicated(bar[,1])]<-”" ## don’t repeat variable names
bar[,2]<-gsub(”.*?\\.”,”",names(unlist(foo))) ## second column has factor levels
bar[,3]<-as.numeric(unlist(foo))
bar[,3][is.na(bar[,3])]<-0 ## in case anything has been coerced
colnames(bar)<-c(”Item”,”Response Category”,”Number”)
rownames(bar)<-rep(”",nrow(bar))
bar<-xtable(bar)
align(bar)<-c(”l”,”l”,”p{7cm}”,”r”) ## make sure factor levels don’t take up more than 7cm
print(bar,tabular.environment = “longtable”,include.rownames=FALSE,size=”footnotesize”,floating=FALSE, #
hline.after=NULL, #
add.to.row=list(pos=list(-1,0, nrow(bar)), #
command=c(’\\toprule ‘, # use nice booktabs formatting
‘\\midrule \\endhead ‘, # with running headers from longtable
‘\\bottomrule ‘)))
Remember to declare that you’re using booktabs and longtable. Here’s an example of data-frame summary.
