# Formatting data tables with Grid + Dividers vs. SplitBy --> GridBy?

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

Grid@ColSel[
PACE["PRE"], {"VISIT_ID", "PHYSICIAN_ID", "PT_DROPOUT_Q",
"DUAL_MONITOR_Q", "VOICE_RECOG_Q", "COMPANION_Q",
"CAM_TRUNCATED_Q"}] /. {0 -> ""}


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

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 /@
SplitBy[ColSel[
PACE["PRE"], {"VISIT_ID", "PHYSICIAN_ID", "PT_DROPOUT_Q",
"DUAL_MONITOR_Q", "VOICE_RECOG_Q", "COMPANION_Q",
"CAM_TRUNCATED_Q"}] /. {0 -> ""},
#[[2]] &])


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

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?

-

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:

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

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.

-
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