CrossTable
Cross tabulation (Pivot table) utility to convert table data to a pivot table form.
Source (List table)
| os | lang | price |
|---|---|---|
| win | ruby | 123 |
| linux | ruby | 12 |
| mac | php | 270 |
| win | java | 560 |
| win | php | 750 |
| linux | java | 950 |
| win | java | 1200 |
| win | php | 500 |
| mac | php | 10 |
| mac | java | 566 |
| win | ruby | 210 |
>>>
Destination (Pivot table)
| ruby | java | php | |
|---|---|---|---|
| win | 333 | 1760 | 1250 |
| linux | 12 | 950 | 0 |
| mac | 0 | 566 | 280 |
The test code to get the sample table above.
Installation
Add this line to your application's Gemfile:
gem 'cross_table'And then execute:
$ bundle
Or install it yourself as:
$ gem install cross_table
Usage
Records Count
require 'cross_table'
cross_tbl = CrossTable.counts_from(
recs: [], # Source List Table(Enumerable)
group_rules: [], # Grouping Rules (Array of Hash)
y_keys: [], # Y-axis Keys (Array)
x_keys: [] # X-axis Keys (Array)
ret_type: nil # Type of returned Pivot table. (nil|:xy_titles|:data_only)
)Sample test code of CrossTable.counts_from
Sample data and group_rule setting
Fields Summary
require 'cross_table'
cross_tbl = CrossTable.sums_from(
recs: [], # Source List Table(Enumerable of Hash)
field: :fld_name # Target field name to be summarised
group_rules: [], # Grouping Rules (Array of Hash)
y_keys: [], # Y-axis Keys (Array)
x_keys: [] # X-axis Keys (Array)
ret_type: nil # Type of returned Pivot table. (nil|:xy_titles|:data_only)
)Sample test code of CrossTable.sums_from
Sample data and group_rule setting
Fields Average
require 'cross_table'
cross_tbl = CrossTable.avgs_from(
recs: [], # Source List Table(Enumerable of Hash)
field: :fld_name # Target field name to be summarised
group_rules: [], # Grouping Rules (Array of Hash)
y_keys: [], # Y-axis Keys (Array)
x_keys: [] # X-axis Keys (Array)
ret_type: nil # Type of returned Pivot table. (nil|:xy_titles|:data_only)
)Sample test code of CrossTable.avgs_from
Sample data and group_rule setting
Arbitrary property of grouped records
require 'cross_table'
cross_tbl = CrossTable.from(
recs: [], # Source List Table(Enumerable of Hash)
group_rules: [], # Grouping Rules (Array of Hash)
y_keys: [], # Y-axis Keys (Array)
x_keys: [] # X-axis Keys (Array)
ret_type: nil # Type of returned Pivot table. (nil|:xy_titles|:data_only)
&aggr_proc # Proc/block to get a value from the arg "group_recs"
)Sample test code of CrossTable.from to get a Minumum value.
Sample data and group_rule setting
Multiple Keys Grouping
Sample test code of using multilpe keys.
Development
To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/masa-kunikata/cross_table.
License
The gem is available as open source under the terms of the MIT License.