[R] making a wider, shorter, 4-column table instead of the narrower, longer, 2-column table I get with tm, Hmisc, and Sweave

Christopher W. Ryan cryan at binghamton.edu
Thu Sep 19 21:52:16 CEST 2013


I think my question sort of straddles two mailing lists, texhax and
r-help. Response so far on texhax has been scant.

Using the tm package, I am tabulating the frequencies of words used by
respondents to several survey questions. I use Sweave and the Hmisc
latex() command to produce the output report. running R 2.15.2, MikTeX
2.9, on WinXP.

Here is some Rnw code to replicate the problem:

\documentclass{article}
\usepackage[margin=1in]{geometry}
\usepackage{Sweave}

\begin{document}

<<options, echo=FALSE, results=hide>>=
options(SweaveSyntax="SweaveSyntaxNoweb")
library(tm)
library(Hmisc)
@

<<generatedocuments, echo=FALSE>>=
tags <- sample(1:40, 110, replace=TRUE)
words <- paste("word", tags, sep="")
words2 <- paste(words, tags, sep="")
n <- length(words)
words3 <- paste(words2[1:(n/2)], words2[(n/2 + 1):n], sep=" ")
docs <- data.frame(words3)
# the data.frame part isn't really needed for
# this minimal example, but I include it to
# mimic my operational code
@

<<makecorpora, echo=FALSE, results=hid>>=
docs2 <- Corpus(DataframeSource(docs))
@

<<maketdm, echo=FALSE, results=hide>>=
docs3 <- TermDocumentMatrix(docs2)
@

<<wordfrequencies, echo=FALSE, results=hide>>=
word.freq <- rowSums(inspect(docs3))
@

<<narrowtable, results=tex, echo=FALSE>>=
latex(sort(word.freq, decreasing=TRUE), file="", caption="Frequencies of
words", label="qol" )
@

\end{document}

The output consists, of course, of a very long two-column table, with
wide swaths of empty white space on either side. I'd like to use the
page width more efficiently, by making a shorter, wider table of 4
columns (2 pairs of columns), with the rows  "flowing" from the bottom
of the left-hand pair to the top of the right-hand pair.

I know I could modify the intermediate .tex file "by hand," after
R/Sweave/HMisc generates it, splitting it at some row roughly halfway
down. But is there a way to do this programmatically in the Rnw file?
All I can think of is using two separate chunks to generate two separate
tables, one for elements 1:(length(word.freq)/2) of word.freq and one
for the remaining elements. Is there a better way?

Thanks.

--Chris
-- 
Christopher W. Ryan, MD, MS
SUNY Upstate Medical University Clinical Campus at Binghamton
425 Robinson Street, Binghamton, NY  13904
cryanatbinghamtondotedu

"Once we recognize that we do not err out of laziness, stupidity, or
evil intent, we can liberate ourselves from the impossible burden of
trying to be permanently right. We can take seriously the proposition
that we could be in error, without deeming ourselves idiotic or
unworthy." [Karen Schulz, in Being Wrong: Adventures in the Margin of Error]



More information about the R-help mailing list