Quickly explore your data with `uniq` and `tally`

A common question you may want to answer on user-input data is: what values have been entered and how many times is each one used?

Maybe you have a list of dropdown options and you want to investigate removing a rare-used option.

Ruby has two handy m…


This content originally appeared on DEV Community and was authored by matt swanson

A common question you may want to answer on user-input data is: what values have been entered and how many times is each one used?

Maybe you have a list of dropdown options and you want to investigate removing a rare-used option.

Ruby has two handy methods that I reach for often: uniq and tally.

Usage

The uniq method operates on an enumerable and compresses your data down to unique values.

Outreach::Task.all.map(&:status).uniq
=> ["Confirmed w/o Outreach",
 "Awaiting Outreach",
 "Responded",
 "No Response Expected",
 "Follow-up",
 "Awaiting Reply"]

While most developers are familiar with uniq, the tally method is one of the best kept secrets in Ruby. The tally method takes an enumerable of values and returns a hash where the keys are unique values and the values are the number of times the value appeared in the list.

Outreach::Task.all.map(&:status).tally
=> {"Confirmed w/o Outreach"=>106,
 "Awaiting Outreach"=>28,
 "Responded"=>48,
 "No Response Expected"=>10,
 "Follow-up"=>4,
 "Awaiting Reply"=>8}

These two methods are great to have in your toolbox to quickly explore your data in a Rails console.

Additional Resources

Ruby API: Enumerable#uniq

Ruby API: Enumerable#tally


This content originally appeared on DEV Community and was authored by matt swanson


Print Share Comment Cite Upload Translate Updates
APA

matt swanson | Sciencx (2021-05-05T13:00:00+00:00) Quickly explore your data with `uniq` and `tally`. Retrieved from https://www.scien.cx/2021/05/05/quickly-explore-your-data-with-uniq-and-tally/

MLA
" » Quickly explore your data with `uniq` and `tally`." matt swanson | Sciencx - Wednesday May 5, 2021, https://www.scien.cx/2021/05/05/quickly-explore-your-data-with-uniq-and-tally/
HARVARD
matt swanson | Sciencx Wednesday May 5, 2021 » Quickly explore your data with `uniq` and `tally`., viewed ,<https://www.scien.cx/2021/05/05/quickly-explore-your-data-with-uniq-and-tally/>
VANCOUVER
matt swanson | Sciencx - » Quickly explore your data with `uniq` and `tally`. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/05/05/quickly-explore-your-data-with-uniq-and-tally/
CHICAGO
" » Quickly explore your data with `uniq` and `tally`." matt swanson | Sciencx - Accessed . https://www.scien.cx/2021/05/05/quickly-explore-your-data-with-uniq-and-tally/
IEEE
" » Quickly explore your data with `uniq` and `tally`." matt swanson | Sciencx [Online]. Available: https://www.scien.cx/2021/05/05/quickly-explore-your-data-with-uniq-and-tally/. [Accessed: ]
rf:citation
» Quickly explore your data with `uniq` and `tally` | matt swanson | Sciencx | https://www.scien.cx/2021/05/05/quickly-explore-your-data-with-uniq-and-tally/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.