I have a table like this:
| List | Apple red | Apple yellow | Banana red | Banana yellow |
| a | 1 | 11 | 21 | 31 |
| b | 2 | 12 | 22 | 32 |
| c | 3 | 13 | 23 | 33 |
I would like to stack the data with multiple series (here is apple and banana), but only want 1 label for all series (here is red and yellow). The output is like:
| List | Apple | Banana | group |
| a | 1 | 21 | red |
| a | 11 | 31 | yellow |
| b | 2 | 22 | red |
| b | 12 | 32 | yellow |
| c | 3 | 23 | red |
| c | 13 | 33 | yellow |
I tried stack function but I think this need some special script. Thank you so much!
This can be done in three operations with no scripting.
First, use Tables->Stack to stack your four columns (Apple red, Apple yellow, Banana red, Banana yellow) into a single column.
You'll end up with this:

Now, use Cols->Utlities->Text to Columns to separate the fruit from the color in your Label column. Put a space in the delimiter field.

You'll end up with two new columns. Rename them Fruit and Color. You can delete the Label column.

Now use Tables->Split to split the Data column by Fruit.

Now you'll get the data table you want with, an Apple column and Banana column.

If you Stack the data, then create a new column for the Fruit, and another one for the Color, you can then Split the data into the required data table.
The script below illustrates how to accomplish this. However, doing the steps interactively is how I build the script, so you can easily repeat the interactive steps.
Names Default To Here( 1 );
// Create the sample data table
dt = New Table( "Example",
Add Rows( 3 ),
New Script(
"Source",
Data Table( "Untitled 67" ) << Split(
Split By( :Column 2 ),
Split( :Column 1 ),
Sort by Column Property
)
),
New Column( "List", Character, "Nominal", Set Values( {"a", "b", "c"} ) ),
New Column( "Apple red", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [1, 2, 3] ) ),
New Column( "Apple yellow", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [11, 12, 13] ) ),
New Column( "Banana red", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [21, 22, 23] ) ),
New Column( "Banana yellow", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [31, 32, 33] ) )
);
// Stack the columns
dtStack = dt << Stack(
columns( :Apple red, :Apple yellow, :Banana yellow, :Banana red ),
Source Label Column( "Label" ),
Stacked Data Column( "Data" )
);
// Create 2 New Columns separating the fruit from the color
dtStack << New Column("Fruit", character, formula(word(1,:Label," ")));
dtStack << New Column("Color", character, formula(word(2,:Label," ")));
// Split the data back into the required new columns
dtFinal = dtStack << Split(
Split By( :Fruit ),
Split( :Data ),
Remaining Columns( Drop( :Label ) ),
Sort by Column Property
);
