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
, Developer`PartitionMap
or
the (undocumented) 6-argument form of PartititionPartition
to get the first column:
bins2 = MovingMap[Row[{First@#, Last@# - 1}, "-"] &, binlims, 1]
bins3 = Developer`PartitionMap[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 Row
as follows:
bins5 = Partition[binlims, 2, 1, {1, -1}, {}, StringRiffle[ToString/@{#, #2 - 1}, " - "]&]
TableForm[Transpose[{bins5, bincounts}]]