你好,
我有一個大型數據表,其中的列有一個 ID 號,後面跟著下面的單詞,
| 1-日期 | 1 參數 | 1東西 | 2東西 | 2-日期 | 2參數 | 3參數 | 3-日期 | 3東西 | 3-否則 |
| | | | | | | | | | |
| | | | | | | | | | |
我的最終目標是自動化以日期為樣本標籤的控製圖。 每個 ID 的列數不統一,也不按日期升序排列。
我的想法是按 ID 將大型數據表拆分為較小的數據表,然後從中製作控製圖。 但是,我不知道如何以自動方式進行這種拆分。
謝謝
如果您的表格沒有規則的列模式,Jim 的解決方案是我提到的更通用的解決方案。 我點擊 Post 後,Jum 的回复才出現。
查爾斯,
如果您的列有常規系列 3(日期、參數名稱、數據值),這可以通過 UI 使用 Main Menu > Tables > Stack 並選擇多系列堆棧 3 並逐行取消選中堆棧來完成。它需要對列名進行一些清理,但結果表可用於使用參數名稱或數據組作為 By 變量的控製圖。
或者,如果您想要單獨的表,您可以在這個新表上使用表子集,選中子集依據,然後指定數據組。
如果您的表沒有重複的系列順序,則需要一個腳本或大量手動工作來對每個組進行子集化。 如果您的數據表的列順序不規則,請回复此帖子。
我附上了一個 JSL 腳本,如果它具有重複的系列列並且所有日期和所有東西都是相同的建模類型(可能是數字的、連續的),它應該適用於您的表。
Names Default To Here(1);
dt = current data table();
cnames = dt << get column names("String");
stk = dt << Stack(
columns(
evalList(cnames)
),
Source Label Column( "Label" ),
Stacked Data Column( "Data" ),
Stack By Row( 0 ),
Number of Series( 3 )
);
stk:Data << Set Name("Date");
stk:Data 2 << Set Name("Parameter Name");
stk:Data 3 << Set Name("Data");
stk:Label << set each value( trim(word(1,:Label,"-")));
stk:Label << Set Name( "Data Group");
stk << delete columns(:Label 2, Label 3);
沒有更多的細節,我只能給出一個大致的方向來解決這個問題。我看到有兩種方法可以滿足您的需求。首先是找出有多少個不同的 ID,並將列放入 ID 的不同列表中,執行以下操作:
Names Default To Here( 1 );
dt = Current Data Table();
colNamesList = dt << get column names( string );
// Create an ID list
listOfIDs = {};
For( i = 1, i <= N Items( colNamesList ), i++,
If( Contains( listOfIDs, Word( 1, Column( dt, colNamesList[i] ) << get name, "-" ) ) == 0,
// Insert into the listOfIDs, the new ID name
Insert Into( listOfIDs, Word( 1, Column( dt, colNamesList[i] ) << get name, "-" ) );
// Create a list to hold the column names for the new ID
Eval( Parse( "listOfColumns_" || Char( N Items( listOfIDs ) ) || " = {};" ) );
Eval(
Parse(
"insert into(listOfColumns_" || Char( N Items( listOfIDs ) ) || "," || Char(
Column( dt, colNamesList[i] ) << get name
) || ");"
)
);
,
// If the ID has already been see, then just add the column name to the
// ID list of column names
Eval(
Parse(
"insert into(listOfColumns_" || Char(
Contains( listOfIDs, Word( 1, Column( dt, colNamesList[i] ) << get name, "-" ) )
) || "," || Char( Column( dt, colNamesList[i] ) ) << get name || ");"
)
)
)
);
然後,您可以遍歷 listOfIDs 並進行處理。
另一種方法是堆疊所有列,然後為 ID 創建一個新列,從標籤中去除 ID 值,然後將列拆分回公共列,並使用 By 語句進行處理以分隔不同的身份證。就像是
Names Default To Here( 1 );
dt = Current Data Table();
colNamesList = dt << get column names( string );
// Stack the columns
dtStack = dt << Stack(
columns( colNamesList ),
Source Label Column( "Label" ),
Stacked Data Column( "Data" ),
Drop All Other Columns( 1 )
);
// Create the ID Column
dtStack << New Column("ID", character,formula( word(1,:label,"-")));
// Now delete the ID from the :Label column
For Each Row(
:Label = trim(substr( :Label, contains(:Label,"-")+1));
);
// Split back to common columns
dtFinal = dtStack << Split(
Split By( :Label ),
Split( :Data ),
Sort by Column Property
);
現在讓我澄清一下......正如我所說,由於你的細節很少,我對此做了很多猜測。此外,如果沒有示例數據表,我提供的代碼還沒有經過測試,我認為它不會在第一次執行時工作。它旨在顯示您可以採取的解決問題的可能方向