From 48ba9ba595b013e3679f305dedecbf86614ccfaf Mon Sep 17 00:00:00 2001 From: Jud Dagnall Date: Wed, 2 Oct 2024 23:24:51 -0700 Subject: [PATCH 1/4] Add an inital MeltGuide --- visidata/guides/MeltGuide.md | 62 ++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 visidata/guides/MeltGuide.md diff --git a/visidata/guides/MeltGuide.md b/visidata/guides/MeltGuide.md new file mode 100644 index 000000000..52fad6a96 --- /dev/null +++ b/visidata/guides/MeltGuide.md @@ -0,0 +1,62 @@ +--- +sheettype: Sheet +--- +# Melt is just unpivot + +Pivot tables can be melted (or unpivoted) into regular row and column data in "long" format. The pivoted columns get flattened into a 'Variable' column, and the cell content become the 'Value' for each row. + +See the **PivotGuide** for how to create pivot tables. + +See **Examples** below for more details of the transformations + +## Melt + +Melt collapses the pivoted columns back into "Variable" and "Value" columns, converting from "wide" format into "long" format. After a melt, there is one row +for each value in the pivot table. + +- {help.commands.melt} + +## Regex-Melt + +A regex melt partially melts the pivoted columns. Using a regular expression, + +- {help.commands.melt_regex} + +## Examples + +Start with a pivot table, created by pivoting a table with color column (with R and B), and two aggregators `sum` and `mean` + + date sum_R mean_R sum_B mean_B + ---------- ----- ------ ----- ------ + 2024-09-01 30 30 0 + 2024-09-02 0 28 28 + 2024-09-03 100 100 132 66 + +### Standard `melt`: + + date Variable Value + ---------- -------- ----- + 2024-09-01 sum_R 30 + 2024-09-01 mean_R 30 + 2024-09-01 sum_B 0 + 2024-09-02 sum_R 0 + 2024-09-02 sum_B 28 + 2024-09-02 mean_B 28 + 2024-09-03 sum_R 100 + 2024-09-03 mean_R 100 + 2024-09-03 sum_B 132 + 2024-09-03 mean_B 66 + +### Big melt: melt-regex + +Use the `melt-regex` of `(\w+)_(\w)` to separate the aggregator from the color. Only the aggregator will be fully melted. The colors will remain as columns: + + date Variable B R + ---------- -------- --- --- + 2024-09-01 sum 0 30 + 2024-09-01 mean 30 + 2024-09-02 sum 28 0 + 2024-09-02 mean 28 + 2024-09-03 sum 132 100 + 2024-09-03 mean 66 100 + From a504873cc3eaf99bdb90c228f522a5ee101ffdee Mon Sep 17 00:00:00 2001 From: Jud Dagnall Date: Wed, 2 Oct 2024 23:56:35 -0700 Subject: [PATCH 2/4] Add additional options --- visidata/guides/MeltGuide.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/visidata/guides/MeltGuide.md b/visidata/guides/MeltGuide.md index 52fad6a96..2ee336cc6 100644 --- a/visidata/guides/MeltGuide.md +++ b/visidata/guides/MeltGuide.md @@ -14,11 +14,17 @@ See **Examples** below for more details of the transformations Melt collapses the pivoted columns back into "Variable" and "Value" columns, converting from "wide" format into "long" format. After a melt, there is one row for each value in the pivot table. +1. [Optional] remove a column from the output by hiding it: [:keys]-[/] +2. [Optional] set key columns to keep them unmelted: [:keys]![/] + - {help.commands.melt} ## Regex-Melt -A regex melt partially melts the pivoted columns. Using a regular expression, +A regex melt partially melts the pivoted columns. Provide a regular expression to separate the key column from other columns. + +1. [Optional] remove a column from the output by hiding it: [:keys]-[/] +2. [Optional] set key columns to keep them unmelted: [:keys]![/] - {help.commands.melt_regex} From f18d9b95f6a4d9802db7bfe5fb02b04ed5b9c23b Mon Sep 17 00:00:00 2001 From: Jud Dagnall Date: Thu, 3 Oct 2024 00:05:06 -0700 Subject: [PATCH 3/4] Fix formatting and puctuation. --- visidata/guides/MeltGuide.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/visidata/guides/MeltGuide.md b/visidata/guides/MeltGuide.md index 2ee336cc6..70d9b0904 100644 --- a/visidata/guides/MeltGuide.md +++ b/visidata/guides/MeltGuide.md @@ -7,12 +7,11 @@ Pivot tables can be melted (or unpivoted) into regular row and column data in "l See the **PivotGuide** for how to create pivot tables. -See **Examples** below for more details of the transformations +See **Examples** below for more details of the transformations. ## Melt -Melt collapses the pivoted columns back into "Variable" and "Value" columns, converting from "wide" format into "long" format. After a melt, there is one row -for each value in the pivot table. +Melt collapses the pivoted columns back into "Variable" and "Value" columns, converting from "wide" format into "long" format. After a melt, there is one row for each value in the pivot table. 1. [Optional] remove a column from the output by hiding it: [:keys]-[/] 2. [Optional] set key columns to keep them unmelted: [:keys]![/] From 53f3c66e60f3957dfcd21f5fc54861a26230df0b Mon Sep 17 00:00:00 2001 From: anjakefala Date: Sun, 6 Oct 2024 18:35:33 -0700 Subject: [PATCH 4/4] Clean up melt --- visidata/guides/MeltGuide.md | 25 +++++-------------------- 1 file changed, 5 insertions(+), 20 deletions(-) diff --git a/visidata/guides/MeltGuide.md b/visidata/guides/MeltGuide.md index 70d9b0904..4c8652a57 100644 --- a/visidata/guides/MeltGuide.md +++ b/visidata/guides/MeltGuide.md @@ -3,33 +3,20 @@ sheettype: Sheet --- # Melt is just unpivot -Pivot tables can be melted (or unpivoted) into regular row and column data in "long" format. The pivoted columns get flattened into a 'Variable' column, and the cell content become the 'Value' for each row. - -See the **PivotGuide** for how to create pivot tables. - -See **Examples** below for more details of the transformations. - -## Melt - Melt collapses the pivoted columns back into "Variable" and "Value" columns, converting from "wide" format into "long" format. After a melt, there is one row for each value in the pivot table. 1. [Optional] remove a column from the output by hiding it: [:keys]-[/] 2. [Optional] set key columns to keep them unmelted: [:keys]![/] -- {help.commands.melt} - -## Regex-Melt +3a. {help.commands.melt} A regex melt partially melts the pivoted columns. Provide a regular expression to separate the key column from other columns. -1. [Optional] remove a column from the output by hiding it: [:keys]-[/] -2. [Optional] set key columns to keep them unmelted: [:keys]![/] - -- {help.commands.melt_regex} +3b. {help.commands.melt_regex} ## Examples -Start with a pivot table, created by pivoting a table with color column (with R and B), and two aggregators `sum` and `mean` +Start with a pivot table, created by pivoting a table with columns `R`,`B`, and key column `date`. There are two aggregators `sum` and `mean`: date sum_R mean_R sum_B mean_B ---------- ----- ------ ----- ------ @@ -37,7 +24,7 @@ Start with a pivot table, created by pivoting a table with color column (with R 2024-09-02 0 28 28 2024-09-03 100 100 132 66 -### Standard `melt`: +Press `Shift+M` for a standard melt: date Variable Value ---------- -------- ----- @@ -52,9 +39,7 @@ Start with a pivot table, created by pivoting a table with color column (with R 2024-09-03 sum_B 132 2024-09-03 mean_B 66 -### Big melt: melt-regex - -Use the `melt-regex` of `(\w+)_(\w)` to separate the aggregator from the color. Only the aggregator will be fully melted. The colors will remain as columns: +Or press `gShift+M` and then type a `regex` of `(\w+)_(\w)` to separate the aggregator from the column. Only the aggregator will be fully melted. The columns `R` and `B` will remain: date Variable B R ---------- -------- --- ---