Simple gem to calculate mean, median and mode from an array.
Add this line to your application's Gemfile:
gem 'average'
And then execute:
$ bundle
Or install it yourself as:
$ gem install average
Get Median
$ [3, 4, 5, 2, 3, 1].mean
$ 3.0
Get Mode
$ [2,4,6,14,21,24,229,24,24,39].mode
$ 24
It gives back an array due that could be possible to have more than one mode in a array
$ [1, 2, 2, 2, 3, 3, 7, 7, 7, 9].mode
$ [2, 7]
But if you want to have only a unique mode, you can use 'unique_mode' method (Only for Ruby versions bigger than 1.8.7 -> http://apidock.com/ruby/v1_8_7_72/Enumerable/max_by).
$ [1, 2, 2, 2, 3, 3, 7, 7, 7, 9].unique_mode
$ 2
Get Median
$ [3,4,5].median
$ 4
All methods described aboved can handle an array of integers, floats, strings or a mixed array with all of them
$ [3, "4", 5, 2.0, 3, "1.0"].mean
$ 3.0
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request