我有一個這樣的表:
| 清單 | 蘋果紅 | 蘋果黃 | 香蕉紅 | 香蕉黃 |
| A | 1 | 11 | 21 | 31 |
| 乙 | 2 | 12 | 22 | 32 |
| C | 3 | 13 | 23 | 33 |
我想將數據堆疊到多個系列(這裡是蘋果和香蕉),但只想要所有系列有 1 個標籤(這裡是紅色和黃色)。 我還想保留標題的左側(這裡是蘋果和香蕉)。 輸出如下:
| 清單 | 蘋果 | 香蕉 | 團體 |
| A | 1 | 21 | 紅色的 |
| A | 11 | 31 | 黃色的 |
| 乙 | 2 | 22 | 紅色的 |
| 乙 | 12 | 32 | 黃色的 |
| C | 3 | 23 | 紅色的 |
| C | 13 | 33 | 黃色的 |
我嘗試了堆疊函數,但我認為這需要一些特殊的腳本。 太感謝了!
如果您堆疊數據,然後為水果建立一個新列,為顏色建立另一列,然後您可以將數據拆分到所需的數據表中。
下面的腳本說明如何完成此操作。但是,我建立腳本的方式是以互動方式執行這些步驟,因此您可以輕鬆地重複互動步驟。
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
);

這可以透過三個操作來完成,無需編寫腳本。
首先,使用表->堆疊堆疊四列(蘋果紅、蘋果黃、香蕉紅、香蕉黃)到單列中。
你最終會得到這樣的結果:

現在,使用列->實用程式->文字到列將水果與標籤列中的顏色分開。 在分隔符號欄位中放置一個空格。

您最終將得到兩個新列。 重新命名它們水果和顏色。 您可以刪除標籤柱子。

現在使用表格->拆分來分割數據列依據水果。

現在您將獲得您想要的數據表,蘋果列和香蕉柱子。
