Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

A quick method to format human-readable data tables with headers, is to use Grid, eg:

    "CAM_TRUNCATED_Q"}] /. {0 -> ""} 

Which yields this table of which only the top portion is shown (note the horizontal spacing induced by the header)

enter image description here

It is desirable to segment the data by PHYSICIAN_ID, which can have variable number of visits. Is there a convenient expression to map such a rule to positional information required by Grid and Dividers?

A more verbose alternative is to space by first applying SplitBy #[2] (2nd column = PHYSICIAN_ID), then Grid, and finally Column:

Column[#, Automatic, 2] & @(Grid /@ 
       "CAM_TRUNCATED_Q"}] /. {0 -> ""},
    #[[2]] &])

Which yields (again only showing data for the first 3 physicians):

enter image description here

Vertical spacing is visually less satisfactory than horizontal grid lines (which visually guide the eye). Also note the header has been split from the data so the vertical alignment is lost.

So overall Grid and Dividers is preferable; What are the issues in developing a "GridBy" functionality analogous to SplitBy?

share|improve this question
up vote 6 down vote accepted

You can accomplish this by mapping Unequal onto pairs of adjacent entries in the PHYSICIAN_ID column to construct a list that tells Grid where to put horizontal rules. Here is an example:

  thegrid = {{"A", "One"}, {"A", "Two"}, {"A", "Three"}, {"B", 
     "Four"}, {"B", "Five"}, {"C", "Six"}, {"D", "Seven"}},
  splitColumn = 1,

 dividerQ = 
  thegrid[[All , splitColumn]] //
    Partition[#, 2, 1] & //
   Apply[Unequal, #, {1}] &;

 dividerQ = Prepend[dividerQ, False];

 Grid[thegrid, Dividers -> {False, dividerQ}]

Change splitColumn to the index of the column you want to use, which should be 2 in your case.

share|improve this answer
Thanks DGrady, that works. Fyi, the above can be inlined: Dividers -> {False, Prepend[Unequal[#[[1]], #[[2]]] & /@ Partition[#[[All, 2]], 2, 1], False]} – alancalvitti Jul 12 '12 at 21:10

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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