How to group numeric data into ranges in Rails -
in rails 3.2 app have following model
create_table "people", |t| t.integer "height" end
the height
attribute person's height in cm.
i want group data in table 5cm height classes, , return count each height class. e.g.,
{"height_classes":[150-155, 155-160, 160-165],"height_counts":[1, 3, 11]}
how can group classes this?
so far i've used following:
step = 5 data = people.all.sort_by &:length height_classes = data.map{ |d| d.height - (d.length % step) }.uniq height_counts = height_classes.map{ |hc| data.select{ |d| d.length - (d.length % step) == l }.count}
but not meeting needs. achieve is:
- the height_classes run minimum maximum values in height column (but adjusted 5cm class - i.e. if minimum value 172 grouping should 170-175)
- any empty classes should included, count of zero
- the
height_classes
should string displaying range of values
is possible?
Comments
Post a Comment