This example shows how to stack variables by using the Stack Table Variables Live Editor task. It takes values from multiple variables in an input table, and combines them into one variable in an output table.
First, load a table from a sample file that lists the number of power company customers who experienced electric power outages. The sample data are aggregated by month. The power company serves three regions in the United States. So there are three variables, NorthEast
, MidWest
, and SouthEast
, that list the numbers of customers affected in each region.
customersByRegion=6×4 table
Month NorthEast MidWest SouthEast
_____________ _________ _______ _________
October-2023 3492 565 1027
November-2023 2944 1293 834
December-2023 2559 936 1412
January-2024 12045 50117 0
February-2024 4931 1089 137
March-2024 3018 942 870
Stack the customer outage data so that the numbers from the NorthEast
, MidWest
, and SouthEast
variables are in one variable in the output table.
Add the Stack Table Variables task to your live script.
To copy values from the Month
variable to rows of the output table, select Constant
from the drop-down list above it in the task.
To combine the values from the NorthEast
, MidWest
, and SouthEast
variables by stacking them into one variable of the output, select Stack
for each variable.
The name of the stacked variable in the output comes from combining the input variable names.
The task also stacks the names of the input variables into a new variable, named NorthEast_MidWest_SouthEast_Indicator
, that indicates the regions for the customer outage data.
stackedTable=18×3 table
Month NorthEast_MidWest_SouthEast_Indicator NorthEast_MidWest_SouthEast
_____________ _____________________________________ ___________________________
October-2023 NorthEast 3492
October-2023 MidWest 565
October-2023 SouthEast 1027
November-2023 NorthEast 2944
November-2023 MidWest 1293
November-2023 SouthEast 834
December-2023 NorthEast 2559
December-2023 MidWest 936
December-2023 SouthEast 1412
January-2024 NorthEast 12045
January-2024 MidWest 50117
January-2024 SouthEast 0
February-2024 NorthEast 4931
February-2024 MidWest 1089
February-2024 SouthEast 137
March-2024 NorthEast 3018
⋮
Give the variables of the output table more meaningful names. To rename table variables, use the renamevars
function.
customersOutage=18×3 table
Month Region CustomersOutage
_____________ _________ _______________
October-2023 NorthEast 3492
October-2023 MidWest 565
October-2023 SouthEast 1027
November-2023 NorthEast 2944
November-2023 MidWest 1293
November-2023 SouthEast 834
December-2023 NorthEast 2559
December-2023 MidWest 936
December-2023 SouthEast 1412
January-2024 NorthEast 12045
January-2024 MidWest 50117
January-2024 SouthEast 0
February-2024 NorthEast 4931
February-2024 MidWest 1089
February-2024 SouthEast 137
March-2024 NorthEast 3018
⋮