<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Chris Hanretty &#187; latex</title>
	<atom:link href="http://chrishanretty.co.uk/blog/index.php/category/latex/feed/" rel="self" type="application/rss+xml" />
	<link>http://chrishanretty.co.uk/blog</link>
	<description>Notes on Italian politics and public broadcasting</description>
	<lastBuildDate>Sat, 04 Feb 2012 12:15:14 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Summarizing a data-frame with xtable</title>
		<link>http://chrishanretty.co.uk/blog/index.php/2009/07/02/summarizing-a-data-frame-with-xtable/</link>
		<comments>http://chrishanretty.co.uk/blog/index.php/2009/07/02/summarizing-a-data-frame-with-xtable/#comments</comments>
		<pubDate>Thu, 02 Jul 2009 18:33:02 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[latex]]></category>

		<guid isPermaLink="false">http://chrishanretty.co.uk/blog/index.php/2009/07/02/summarizing-a-data-frame-with-xtable/</guid>
		<description><![CDATA[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&#8217;s no easy way in LaTeX to split wide tables over multiple pages, that approach [...]]]></description>
			<content:encoded><![CDATA[<p>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 <a href="http://cran.r-project.org/web/packages/xtable/index.html">xtable package</a>.</p>
<p>Unfortunately, if you try the natural</p>
<blockquote><p><tt>xtable(summary(data))</tt></p></blockquote>
<p>you get a table that is very, very wide. Since there&#8217;s no easy way in LaTeX to split wide tables over multiple pages, that approach won&#8217;t work.</p>
<p>Here, instead is my trick for pretty-printing such summaries (basically shoving everything into a matrix and using xtable on that). This won&#8217;t work if you have a period in any of your variable names. Kludgy, I know.</p>
<blockquote><p><tt>foo&lt;-sapply(data,summary) ## get summary of each var.<br />
bar&lt;-matrix(nrow=length(unlist(foo)),ncol=3) </tt></p>
<p><tt>bar[,1]&lt;-gsub("\\..*","",names(unlist(foo))) ## first column has variable names<br />
bar[,1][duplicated(bar[,1])]&lt;-"" ## don't repeat variable names<br />
bar[,2]&lt;-gsub(".*?\\.","",names(unlist(foo))) ## second column has factor levels<br />
bar[,3]&lt;-as.numeric(unlist(foo))<br />
bar[,3][is.na(bar[,3])]&lt;-0 ## in case anything has been coerced<br />
</tt></p>
<p><tt> colnames(bar)&lt;-c("Item","Response Category","Number")<br />
rownames(bar)&lt;-rep("",nrow(bar))<br />
bar&lt;-xtable(bar)<br />
align(bar)&lt;-c("l","l","p{7cm}","r") ## make sure factor levels don't take up more than 7cm<br />
print(bar,tabular.environment = "longtable",include.rownames=FALSE,size="footnotesize",floating=FALSE, #<br />
hline.after=NULL, #<br />
add.to.row=list(pos=list(-1,0, nrow(bar)), #<br />
command=c('\\toprule ', # use nice booktabs formatting<br />
'\\midrule \\endhead ', # with running headers from longtable<br />
'\\bottomrule ')))</tt></p></blockquote>
<p>Remember to declare that you&#8217;re using <a href="http://www.ctan.org/tex-archive/macros/latex/contrib/booktabs/">booktabs</a> and <a href="http://www.ctan.org/tex-archive/help/Catalogue/entries/longtable.html">longtable</a>. Here&#8217;s an <a href="http://chrishanretty.co.uk/blog/wp-content/uploads/2009/07/data_frame_summary_new_attemptlyx.pdf" title="Example of data-frame summary">example of data-frame summary.</a></p>
]]></content:encoded>
			<wfw:commentRss>http://chrishanretty.co.uk/blog/index.php/2009/07/02/summarizing-a-data-frame-with-xtable/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

