4
$\begingroup$

I want a table with a single column of data, and with headings along both the rows and the column. One might expect it to work like this:

TableForm[{"one", "two", "three"}, 
 TableHeadings -> {{"$10MM", "$20MM", "$30MM"}, {"col"}}]

But this does not work; it displays the data and the row labels correctly, but it does not label the column. What am I missing?

$\endgroup$
8
  • 2
    $\begingroup$ Your data consists of a single row... try TableForm[List /@ {"one", "two", "three"}, TableHeadings -> {{"$10MM", "$20MM", "$30MM"}, {"col"}}]... $\endgroup$
    – Yves Klett
    Commented May 30, 2014 at 20:26
  • 1
    $\begingroup$ TableForm[List /@ {"one", "two", "three"}, TableHeadings -> {{"$10MM", "$20MM", "$30MM"}, {"col"}}] $\endgroup$ Commented May 30, 2014 at 20:31
  • 1
    $\begingroup$ or {{"one"},{"two"},{"three"}} .. $\endgroup$
    – george2079
    Commented May 30, 2014 at 20:36
  • 1
    $\begingroup$ @belisarius and you even got the upvote earlier! Man, I really seem to be a people person :D $\endgroup$
    – Yves Klett
    Commented May 30, 2014 at 20:39
  • 1
    $\begingroup$ @YvesKlett Me too! $\endgroup$ Commented May 30, 2014 at 20:46

3 Answers 3

6
$\begingroup$

You present a single row vector to TableForm, which is rather hankering for a column vector (or matrix).

Instead of {"one", "two", "three"} you need {{"one"}, {"two"}, {"three"}}. There are many ways to achieve this, one of them List /@ {"one", "two", "three"}:

TableForm[List /@ {"one", "two", "three"}, 
 TableHeadings -> {{"$10MM", "$20MM", "$30MM"}, {"col"}}]

Mathematica graphics

$\endgroup$
2
$\begingroup$

I would prefer:

Grid[{{"", "col"}}~Join~
  Transpose[{{"$10M", "$20M", "$30M"}, {"one", "two", "three"}}], 
 Dividers -> All]

because of its clarity and all the additional Grid-options.

$\endgroup$
1
$\begingroup$

To be different, and to illustrate behavior of TableForm which I previously described here, which is that directions flip with every additional List level:

TableForm[
 {{{"one", "two", "three"}}}, 
 TableHeadings -> {{Column@{"$10MM", "$20MM", "$30MM"}}, {"col"}}
]

There may be a cleaner way to do the headings than using Column but I can't think of it at the moment.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.