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?


1 Answer 1


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.

  • $\begingroup$ Thanks DGrady, that works. Fyi, the above can be inlined: Dividers -> {False, Prepend[Unequal[#[[1]], #[[2]]] & /@ Partition[#[[All, 2]], 2, 1], False]} $\endgroup$ Jul 12, 2012 at 21:10

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.