Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Transpose table

3,364 views
Skip to first unread message

Peeter Luikmel

unread,
May 2, 2005, 8:50:50 AM5/2/05
to

Is there any good way to transpose a table in LaTeX: if I have a LaTeX
source of a table and want to turn its columns into rows and rows into
columns?

Thanks in advance,

Peeter


--------------
The source table looks like this

% latex table generated in R 2.1.0 by xtable 1.2-5 package
% Mon May 02 15:48:58 2005
\begin{table}[ht]
\begin{center}
\begin{tabular}{rrrrrrrrrrrrr}
\hline
& Jan & Feb & Mar & Apr & May & Jun & Jul & Aug & Sep & Oct & Nov &
Dec \\
\hline
1995 & 78.10 & 94.00 & 100.40 & 103.00 & 102.10 & 122.20 & 113.70 &
118.70 & 125.70 & 135.60 & 141.40 & 161.50 \\
1996 & 79.90 & 95.80 & 103.80 & 102.90 & 102.10 & 121.80 & 111.90 &
119.10 & 127.60 & 133.80 & 140.80 & 161.10 \\
1997 & 82.50 & 96.10 & 102.90 & 105.00 & 103.90 & 117.90 & 111.10 &
119.20 & 128.60 & 136.00 & 140.60 & 166.40 \\
1998 & 74.40 & 97.60 & 100.60 & 105.20 & 104.10 & 118.20 & 110.90 &
119.70 & 130.10 & 136.10 & 144.10 & 169.40 \\
1999 & 77.00 & 98.40 & 101.30 & 105.30 & 104.50 & 117.70 & 110.80 &
119.90 & 132.70 & 136.30 & 145.90 & 171.40 \\
2000 & 78.00 & 99.10 & 103.10 & 105.70 & 104.40 & 116.60 & 111.40 &
120.10 & 133.00 & 135.90 & 147.70 & 171.90 \\
2001 & 78.30 & 99.70 & 102.60 & 103.10 & 105.60 & 113.80 & 114.20 &
120.00 & 135.20 & 143.80 & 148.90 & 170.40 \\
2002 & 81.80 & 100.80 & 103.40 & 102.90 & 117.30 & 112.20 & 115.50 &
120.00 & 134.50 & 144.40 & 148.80 & 179.70 \\
2003 & 91.20 & 100.40 & 103.20 & 102.90 & 117.10 & 112.70 & 118.10 &
122.50 & 132.50 & 143.80 & 154.90 & 185.70 \\
2004 & 94.00 & 100.20 & 103.50 & 102.30 & 120.30 & 113.30 & 118.10 &
122.70 & 133.00 & 141.60 & 158.30 & 200.80 \\
\hline
\end{tabular}
\end{center}
\end{table}


Tristan Miller

unread,
May 2, 2005, 11:27:26 AM5/2/05
to
Greetings.

In article <r98c711l0as1lbu6p...@4ax.com>, Peeter Luikmel
wrote:


> Is there any good way to transpose a table in LaTeX: if I have a LaTeX
> source of a table and want to turn its columns into rows and rows into
> columns?

The following awk script assumes that the fields are delimited by spaces
and the rows by newlines, but it should be relatively easy to adapt.

#!/bin/sh
## TRANSPOSE A MATRIX
## ASSUMES ALL LINES HAVE THE SAME NUMBER OF FIELDS
## USAGE : transpose filename
# Source: <news:1993Jan30.150622%40gruc19.nor.chevron.com>

exec awk '
NR == 1 {
n = NF
for (i = 1; i <= NF; i++)
row[i] = $i
next
}
{
if (NF > n)
n = NF
for (i = 1; i <= n; i++)
row[i] = row[i] " " $i
}
END {
for (i = 1; i <= n; i++)
print row[i]
}' ${1+"$@"}

Regards,
Tristan

--
_
_V.-o Tristan Miller [en,(fr,de,ia)] >< Space is limited
/ |`-' -=-=-=-=-=-=-=-=-=-=-=-=-=-=-= <> In a haiku, so it's hard
(7_\\ http://www.nothingisreal.com/ >< To finish what you

Vilar Camara Neto

unread,
May 2, 2005, 2:41:25 PM5/2/05
to
Peeter Luikmel wrote:
>
> Is there any good way to transpose a table in LaTeX: if I have a LaTeX
> source of a table and want to turn its columns into rows and rows into
> columns?

The memoir class has the `\autocols' command. With this, the material
is put first in the rows of the 1st column, then in the 2nd, and so on.
AFAIK, unfortunately it doesn't have any formatting capabilities, such
as putting horizontal/vertical lines, defining column widths and
alignments, etc..

--
Best regards,

Vilar Camara Neto

0 new messages