[R] Rearranging long tables, Sweave, xtable, LaTeX

Ista Zahn istazahn at gmail.com
Thu Nov 12 12:44:34 CET 2009


Well you can add in the column names, but this looks weird unless you
format it as a table heading. I'm not that familiar with xtable, but
you can do it with Hmisc::latex as follows:

> (tmp2 <- rbind(tmp[,1:20], colnames(tmp[,21:40]), tmp[,21:40]))
  a   b   c   d   e   f   g   h   i   j   k   l   m   n   o   p   q   r   s
1 "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1"
2 "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1"
3 "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1"
  "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S"
1 "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1"
2 "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1"
3 "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1"
  t
1 "1"
2 "1"
3 "1"
  "T"
1 "1"
2 "1"
3 "1"
> require(Hmisc)
> latex(tmp2, n.rgroup = c(3,1,3), file="")
% latex.default(tmp2, n.rgroup = c(3, 1, 3), file = "")
%
\begin{table}[!tbp]
 \begin{center}
 \begin{tabular}{lllllllllllllllllllll}\hline\hline
\multicolumn{1}{l}{tmp2}&\multicolumn{1}{c}{a}&\multicolumn{1}{c}{b}&\multicolumn{1}{c}{c}&\multicolumn{1}{c}{d}&\multicolumn{1}{c}{e}&\multicolumn{1}{c}{f}&\multicolumn{1}{c}{g}&\multicolumn{1}{c}{h}&\multicolumn{1}{c}{i}&\multicolumn{1}{c}{j}&\multicolumn{1}{c}{k}&\multicolumn{1}{c}{l}&\multicolumn{1}{c}{m}&\multicolumn{1}{c}{n}&\multicolumn{1}{c}{o}&\multicolumn{1}{c}{p}&\multicolumn{1}{c}{q}&\multicolumn{1}{c}{r}&\multicolumn{1}{c}{s}&\multicolumn{1}{c}{t}\tabularnewline
\hline
&&&&&&&&&&&&&&&&&&&&\tabularnewline
1&1&1&1&1&1&1&1&1&1&1&1&1&1&1&1&1&1&1&1&1\tabularnewline
2&1&1&1&1&1&1&1&1&1&1&1&1&1&1&1&1&1&1&1&1\tabularnewline
3&1&1&1&1&1&1&1&1&1&1&1&1&1&1&1&1&1&1&1&1\tabularnewline
\hline
&&&&&&&&&&&&&&&&&&&&\tabularnewline
&A&B&C&D&E&F&G&H&I&J&K&L&M&N&O&P&Q&R&S&T\tabularnewline
\hline
&&&&&&&&&&&&&&&&&&&&\tabularnewline
1&1&1&1&1&1&1&1&1&1&1&1&1&1&1&1&1&1&1&1&1\tabularnewline
2&1&1&1&1&1&1&1&1&1&1&1&1&1&1&1&1&1&1&1&1\tabularnewline
3&1&1&1&1&1&1&1&1&1&1&1&1&1&1&1&1&1&1&1&1\tabularnewline
\hline
\end{tabular}

\end{center}

\end{table}


On Thu, Nov 12, 2009 at 6:16 AM, Ottorino-Luca Pantani
<ottorino-luca.pantani at unifi.it> wrote:
> Ista Zahn ha scritto:
>>
>> Yeah, I've had this kind of situation before. Usually I first see if I
>> can fit it on the page by rotating it and/or reducing the size. If
>> that doesn't do it then I'll re-arrange as you suggest. Basically the
>> print method is just wrapping the output after n columns (27 in my
>> case, but I think this depends on the options you have set). So you
>> can do this wrapping yourself before calling xtable:
>>
>> tmp <- with(expand.grid(Fact1 = 1:3, Fact2 = 1:40), table(Fact1, Fact2))
>> tmp2 <- rbind(tmp[,1:20], tmp[,21:40])
>> xtable(tmp2)
>>
>> Hope it helps,
>>
>> Ista
>>
>>
>>
>
> Thanks a lot, but still there are problems.
>
> My fault in the too simple code posted.
>
> Consider the following
>
> (tmp <- with(expand.grid(Fact1 = 1:3, Fact2 = c(letters[1:20],
> LETTERS[1:20])), table(Fact1, Fact2))
>
>> Fact2 Fact1 a b c d e f g h i j k l m n o p q r s t A B C D E F G H I 1 1
>> 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1
>> 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 3 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
>> 1 1 1 1 1 1 1 1 1 1 1 1 Fact2 Fact1 J K L M N O P Q R S T 1 1 1 1 1 1 1 1 1
>> 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 3 1 1 1 1 1 1 1 1 1 1 1
>
> From this table I can easily see, in the 1st row (Fact1) which are the
> observations for the minuscule as well for the MAJUSCULE
>
> while in
>
> (tmp2 <- rbind(tmp[,1:20], tmp[,21:40]))
>
>> a b c d e f g h i j k l m n o p q r s t 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
>> 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 3 1 1 1 1 1 1 1 1 1 1 1 1
>> 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1
>> 1 1 1 1 1 1 1 1 1 1 1 1 3 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
>
> the infomation on the MAJUSCULE factor is completely missing (hided).
>
>
>
>
>
>
>



-- 
Ista Zahn
Graduate student
University of Rochester
Department of Clinical and Social Psychology
http://yourpsyche.org




More information about the R-help mailing list