# Formatting a table to look nice

my current code is:

binsize = 21;
data = {535, 481, 554, 567, 565, 513, 526, 506, 565, 475, 552, 533,
474, 556, 520, 508, 597, 479, 537, 499, 546, 473, 579, 526, 594,
477, 518, 538, 497, 565};
firstbin = 472;
a = BinCounts[data, {firstbin, Max[data] + binsize, binsize}];
b = Range[firstbin, Max[data] + binsize, binsize];
Transpose[{Take[b, Length[a]], a}] // TableForm


Which does give me a nice table, however I would like the bins to be labeled something like "472-492 6" instead of just "472 6"

any advice would be great, thank you.

You can use HistogramList to get bin limits and bin counts in one step and process the output to get the desired structure:

{binlims, bincounts} = HistogramList[data, {firstbin, Max[data] + binsize, binsize}];
bins = Row[{#, #2 - 1}, "-"] & @@@ Partition[binlims, 2, 1];
TableForm[Transpose[{bins, bincounts}]]


Alternatively, you can use MovingMap, DeveloperPartitionMap or the (undocumented) 6-argument form of Partition to get the first column:

bins2 = MovingMap[Row[{First@#, Last@# - 1}, "-"] &, binlims, 1]
bins3 = DeveloperPartitionMap[Row[{First@#, Last@# - 1}, "-"] &, binlims, 2, 1];
bins4 = Partition[binlims, 2, 1, {1, -1}, {}, Row[{#, #2 - 1}, "-"] &];
bins == bins2 == bins3 == bins4


True

Finally, you can also use a combination of StringRiffle and ToString in place of Rowas follows:

bins5 = Partition[binlims, 2, 1, {1, -1}, {}, StringRiffle[ToString/@{#, #2 - 1}, " - "]&]
TableForm[Transpose[{bins5, bincounts}]]


• Thank you very much! Mar 6, 2019 at 21:20
• @Wombles, you are welcome.
– kglr
Mar 6, 2019 at 21:22

Here is one way:

bb = b - 1;
c = Complement[bb, {Min[bb]}];
Transpose[{Take[b, Length[a]], ConstantArray["---", Length[a]], c, a}] // TableForm


Here is the output:

With MarcoB's hint, and some experimenting:

c = Complement[b - 1, {Min[b - 1]}];
y = Map[ToString, Take[b, Length[a]]];
z = Map[ToString, c];
Transpose@{Map[StringJoin, Transpose[{y, ConstantArray["---", Length[a]], z}]], a} // TableForm


• I wish I knew more about string manipulation to get the first three columns to combine to one column of text!
– mjw
Mar 6, 2019 at 21:15
• You might want ToString and StringJoin Mar 6, 2019 at 21:53
• @MarcoB, Yes, Thank you! That's what I was looking for! I actually found ToString and figured there was a way of joining the columns ... Probably there is a more efficient way then what I've put together. At least it is a start.
– mjw
Mar 7, 2019 at 1:54
• Row as suggested in kglr's answer, can also work as a substitute for StringJoin.
– mjw
Mar 7, 2019 at 2:51