{"id":13718919,"url":"https://github.com/brianhempel/simple_stats","last_synced_at":"2025-07-05T04:38:19.202Z","repository":{"id":2440550,"uuid":"3410511","full_name":"brianhempel/simple_stats","owner":"brianhempel","description":"Finally, simple mean, median, mode, sum, and frequencies for Ruby arrays and enumerables!","archived":false,"fork":false,"pushed_at":"2013-12-16T03:44:06.000Z","size":108,"stargazers_count":26,"open_issues_count":1,"forks_count":5,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-25T09:44:10.664Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"Lyndir/Gorillas","license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/brianhempel.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2012-02-10T19:57:16.000Z","updated_at":"2024-02-21T21:19:16.000Z","dependencies_parsed_at":"2022-08-20T14:00:35.156Z","dependency_job_id":null,"html_url":"https://github.com/brianhempel/simple_stats","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/brianhempel/simple_stats","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/brianhempel%2Fsimple_stats","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/brianhempel%2Fsimple_stats/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/brianhempel%2Fsimple_stats/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/brianhempel%2Fsimple_stats/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/brianhempel","download_url":"https://codeload.github.com/brianhempel/simple_stats/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/brianhempel%2Fsimple_stats/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":260286524,"owners_count":22986602,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-03T01:00:39.469Z","updated_at":"2025-06-17T03:37:33.796Z","avatar_url":"https://github.com/brianhempel.png","language":"Ruby","funding_links":[],"categories":["Statistics"],"sub_categories":[],"readme":"# SimpleStats [![Build Status](https://secure.travis-ci.org/brianhempel/simple_stats.png)](http://travis-ci.org/brianhempel/simple_stats)\n\n\nInstall without Bundler:\n\n    gem install simple_stats --no-ri --no-rdoc\n\nInstall with Bundler:\n\n```ruby\ngem \"simple_stats\"\n```\n\nThen get some statistics on arrays or enumerables:\n\n```ruby\nrequire 'rubygems' # if not using Bundler\nrequire 'simple_stats'\n\nsamples = [3, 1, 0, 8, 2, 2, 3, 9]\n\nsamples.sum    # 28 \nsamples.mean   # 3.5 \nsamples.median # 2.5 \nsamples.modes  # [2, 3] \nsamples.frequencies\n# {\n#   0 =\u003e 1,\n#   1 =\u003e 1,\n#   2 =\u003e 2,\n#   3 =\u003e 2,\n#   8 =\u003e 1,\n#   9 =\u003e 1\n# }\nsamples.sorted_frequencies\n# most common elements first\n# [\n#   [2, 2],\n#   [3, 2],\n#   [0, 1],\n#   [1, 1],\n#   [8, 1],\n#   [9, 1]\n# ]\n\n# of course, you can take modes/frequencies on non-numeric data...\n\"banana nanny!\".chars.modes\n# [\"n\"]\n\"banana nanny!\".chars.frequencies\n# {\n#   \" \" =\u003e 1,\n#   \"! \"=\u003e 1,\n#   \"a\" =\u003e 4,\n#   \"b\" =\u003e 1,\n#   \"n\" =\u003e 5,\n#   \"y\" =\u003e 1\n# }\n\"banana nanny!\".chars.sorted_frequencies\n# [\n#   [\"n\", 5],\n#   [\"a\", 4],\n#   [\" \", 1],\n#   [\"!\", 1],\n#   [\"b\", 1],\n#   [\"y\", 1]\n# ]\n\n# sum of nothing is 0\n[].sum # 0\n\n# mean and median of nothing is undefined\n[].mean   # nil\n[].median # nil\n\n# modes and frequencies return empty containers\n[].modes              # []\n[].frequencies        # {}\n[].sorted_frequencies # []\n\n# if elements are present, mean and median always return floats\n[1, 2, 3].mean      # 2.0\n[1, 2, 3].median    # 2.0\n[1, 2, 3, 4].mean   # 2.5\n[1, 2, 3, 4].median # 2.5\n\n# sum, mode, and frequencies preserve the object class\n[1, 2, 3].sum       # 6\n[1.0, 2.0, 3.0].sum # 6.0\n\n# but no guarantees on class here:\n[2.0, 2,   1].modes # [2]\n[2,   2.0, 1].modes # [2.0]\n```\n\n## Mapping\n\nIf the thing you want stats on is buried in your objects, you can pass a block to any SimpleStats method.\n\n```ruby\n# these two lines do the same thing\ncities.mean {|city| city.population}\ncities.map  {|city| city.population}.mean\n\n# other examples\ncities.sum(\u0026:public_school_count)\ncities.mean(\u0026:public_school_count)\ncities.frequencies(\u0026:professional_team_count)\n\n# more complicated examples\ncities.median {|city| city.municipal_income / city.schools.sum(\u0026:students)}\ncities.map(\u0026:schools).flatten.modes(\u0026:team_name) # most common school sports team name\ncities.map(\u0026:professional_teams).flatten.sorted_frequencies(\u0026:kind) # number of different kinds of sports teams\n```\n\n## Interaction with other gems\n\nIf any of SimpleStats' methods are already defined on Enumerable, SimpleStats _will not_ replace them with its own definition. In particular, ActiveSupport [defines a `sum` method](https://github.com/rails/rails/blob/master/activesupport/lib/active_support/core_ext/enumerable.rb). If both ActiveSupport and SimpleStats are used, the `sum` method will come from ActiveSupport. Don't worry, ActiveSupport and SimpleStats should work fine together.\n\n## Help make it better!\n\nNeed something added? [Open an issue](https://github.com/brianhempel/simple_stats/issues). Or, even better, code it yourself and send a pull request:\n\n    # fork it on github, then clone:\n    git clone git@github.com:your_username/simple_stats.git\n    bundle install\n    rspec\n    # hack away\n    git push\n    # then make a pull request\n\n## License\n\nPublic domain. (I, Brian Hempel, the original author release this code to the public domain. February 10, 2012.)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbrianhempel%2Fsimple_stats","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbrianhempel%2Fsimple_stats","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbrianhempel%2Fsimple_stats/lists"}