nushell/docs/commands/histogram.md
2020-08-15 07:51:12 -05:00

9.2 KiB

histogram

Creates a new table with a histogram based on the column name passed in.

Syntax: histogram <column_name> ...args

Parameters

  • <column-name>: name of the column to graph by
  • args: column name to give the histogram's frequency column

Examples

Let's say we have this file random_numers.csv which contains 50 random numbers.

Note: The input doesn't have to be numbers it works on strings too. Try it out.

> open random_numbers.csv
────┬────────────────
 #  │ random numbers
────┼────────────────
  05
  12
  20
...
 471
 481
 492
────┴────────────────

If we now want to see how often the different numbers were generated, we can use the histogram function:

> open random_numbers.csv | histogram "random numbers"
───┬────────────────┬─────────────┬────────────┬──────────────────────────────────────────────────────────────────────────────────────────────────────
 # │ random numbers │ occurrences │ percentage │ frequency
───┼────────────────┼─────────────┼────────────┼──────────────────────────────────────────────────────────────────────────────────────────────────────
 008 │ 57.14%     │ *********************************************************
 1114 │ 100.00%    │ ****************************************************************************************************
 229 │ 64.29%     │ ****************************************************************
 336 │ 42.86%     │ ******************************************
 443 │ 21.43%     │ *********************
 5510 │ 71.43%     │ ***********************************************************************
───┴────────────────┴─────────────┴────────────┴──────────────────────────────────────────────────────────────────────────────────────────────────────

We can also set the name of the second column or sort the table:

> open random_numbers.csv | histogram "random numbers" probability
───┬────────────────┬─────────────┬────────────┬──────────────────────────────────────────────────────────────────────────────────────────────────────
 # │ random numbers │ occurrences │ percentage │ probability
───┼────────────────┼─────────────┼────────────┼──────────────────────────────────────────────────────────────────────────────────────────────────────
 008 │ 57.14%     │ *********************************************************
 1114 │ 100.00%    │ ****************************************************************************************************
 229 │ 64.29%     │ ****************************************************************
 336 │ 42.86%     │ ******************************************
 443 │ 21.43%     │ *********************
 5510 │ 71.43%     │ ***********************************************************************
───┴────────────────┴─────────────┴────────────┴──────────────────────────────────────────────────────────────────────────────────────────────────────

> open random_numbers.csv | histogram "random numbers" probability | sort-by probability
───┬────────────────┬─────────────┬────────────┬──────────────────────────────────────────────────────────────────────────────────────────────────────
 # │ random numbers │ occurrences │ percentage │ probability
───┼────────────────┼─────────────┼────────────┼──────────────────────────────────────────────────────────────────────────────────────────────────────
 043 │ 21.43%     │ *********************
 136 │ 42.86%     │ ******************************************
 208 │ 57.14%     │ *********************************************************
 329 │ 64.29%     │ ****************************************************************
 4510 │ 71.43%     │ ***********************************************************************
 5114 │ 100.00%    │ ****************************************************************************************************
───┴────────────────┴─────────────┴────────────┴──────────────────────────────────────────────────────────────────────────────────────────────────────

Of course, histogram operations are not restricted to just analyzing numbers in files, you can also analyze your directories

> ls -la | histogram type | sort-by occurrences
───┬─────────┬─────────────┬────────────┬──────────────────────────────────────────────────────────────────────────────────────────────────────
 # │ type    │ occurrences │ percentage │ frequency
───┼─────────┼─────────────┼────────────┼──────────────────────────────────────────────────────────────────────────────────────────────────────
 0 │ Dir     │           5 │ 4.76%      │ ****
 1 │ Symlink │          28 │ 26.67%     │ **************************
 2 │ File    │         105 │ 100.00%    │ ****************************************************************************************************
───┴─────────┴─────────────┴────────────┴──────────────────────────────────────────────────────────────────────────────────────────────────────