是否可以在巢狀 For 循環中從一個原始表建立多個堆疊表?我想從一個原始資料表自動建立多個堆疊表。我計劃透過建立關鍵字清單來實現此目的,如果原始表中的任何列包含該關鍵字,那麼它將堆疊在新的資料表中。本質上我有一個包含 3 個關鍵字的列表,因此我將創建 3 個堆疊資料表。我的腳本有一個巢狀的 for 循環,用於迭代原始資料表中的所有列,並查看它們是否包含正在索引的關鍵字。請見下文:
groupNames = {"abc", "def", "ghi"};
for (i = 1, i <= nitems(groupNames), i++,
for (j = 1, j <= ncols(Data Table("Original Data")), j++,
if ((contains(Column Name(j), groupNames[i])),
Insert Into(tempStack, Column Name(j));
);
);
tempTable = Stack(
Columns(tempStack),
Source Label Column("Label"),
Stacked Data Column("Data"),
"Non-stacked columns"n (Keep(:tst_temp, :stdf_file)),
);
tempStack = {};
tempTable << set name(char(groupNames[i]) || " Stacked");
);
執行此程式碼時出現的問題是,第一組堆疊得很好,但是當 for 循環索引到 i=2 來查找下一組時,它無法查看原始資料表,而是查看剛剛堆疊(在本例中資料表“abc Stacked”)。
我怎麼能讓我的 for 迴圈查看原始資料表而不是新建立的表。謝謝!
最好的,
考提克馬
自動化和腳本編寫內容組織視窗回覆:是否可以在巢狀 For 迴圈中從一個原始表建立多個堆疊表?您只需指定使用原始資料到堆疊輸入
groupNames = {"abc", "def", "ghi"};
For( i = 1, i <= N Items( groupNames ), i++,
For( j = 1, j <= N Cols( Data Table( "Original Data" ) ), j++,
If( (Contains( Column Name( j ), groupNames[i] )),
Insert Into( tempStack, Column Name( j ) )
)
);
tempTable = data table("Original Data") << Stack(
Columns( tempStack ),
Source Label Column( "Label" ),
Stacked Data Column( "Data" ),
"Non-stacked columns"n( Keep( :tst_temp, :stdf_file ) ),
Output Table( Char( groupNames[i] ) || " Stacked" )
);
tempStack = {};
);回覆:是否可以在巢狀 For 迴圈中從一個原始表建立多個堆疊表?嘿吉姆,
不幸的是我已經嘗試過這個但它似乎不起作用。它仍然感到困惑,並認為我在執行 i=2 迭代時指的是新堆疊的表。我可以看出,因為我新堆疊的表格有 4 列。我收到的錯誤是 aj 索引錯誤,當 j=5 時該錯誤失敗。

作為參考,我使用的是 JMP Pro 17.2。
回覆:是否可以在巢狀 For 迴圈中從一個原始表建立多個堆疊表?我可能會讓您對程式碼進行兩處更改感到困惑,但我只談論其中之一。當您最近進行測試時,您是否使用了該行
tempTable = data table("Original Data") << Stack(
如果您這樣做了,然後出現了您指定的錯誤,我會要求您提供範例資料表,然後我將進一步探討該問題。
回覆:是否可以在巢狀 For 迴圈中從一個原始表建立多個堆疊表?嘿吉姆,
不用擔心,這裡是更新的程式碼和範例資料。按照您建議的更改,它仍然不起作用。
tempStack = {};
GroupNames = {"abc", "def"};
for (i = 1, i <= nitems(GroupNames), i++,
for (j = 1, j <= ncols(Data Table("Sample")), j++,
if ((contains(Column Name(j), GroupNames[i])),
Insert Into(tempStack, Column Name(j));
);
);
colNum = ncols(Data Table("Sample"));
tempTable = Data Table("Sample") << Stack(
Columns(tempStack),
Source Label Column("Label"),
Stacked Data Column("Data"),
"Non-stacked columns"n (Keep(:tst_temp, :stdf_file)),
Invisible(1)
);
tempStack = {};
tempTable << set name(char(GroupNames[i]) || " Stacked");
);讓我知道你發現了什麼。謝謝
回覆:是否可以在巢狀 For 迴圈中從一個原始表建立多個堆疊表?正是 Column Name() 函數弄亂了腳本。它指向當前活動的數據表。因此,我將當前資料表的設定加回原始樣本資料表。
names default to here(1);
tempStack = {};
GroupNames = {"abc", "def"};
for (i = 1, i <= nitems(GroupNames), i++,
Current data table(data table("Sample"));
for (j = 1, j <= ncols(Data Table("Sample")), j++,
if ((contains(Column Name(j), GroupNames[i])),
Insert Into(tempStack, Column Name(j));
);
);
colNum = ncols(Data Table("Sample"));
tempTable = Data Table("Sample") << Stack(
Columns(tempStack),
Source Label Column("Label"),
Stacked Data Column("Data"),
"Non-stacked columns"n (Keep(:tst_temp, :stdf_file)),
Invisible(1)
);
tempStack = {};
tempTable << set name(char(GroupNames[i]) || " Stacked");
);回覆:是否可以在巢狀 For 迴圈中從一個原始表建立多個堆疊表?吉姆,
非常感謝您在這方面的幫助!解釋得很好並且工作完美。
最好的,
考提克馬
回覆:是否可以在巢狀 For 迴圈中從一個原始表建立多個堆疊表?這是另一個對你的腳本進行了大量修改的建議。它使用有點不同的方法,使用 For Each() (在我看來比 For() 更容易閱讀)和關聯數組來收集列組。
Names Default To Here(1);
// Initialize Associative Array with groups and empty lists
groups = ["abc" => {}, "def" => {}];
dt = Open("$DOWNLOADS/Sample.jmp");
colnames = dt << Get Column Names("String");
// Collect columns to each key in associative array
For Each({colname}, colnames,
For Each({{group, val}}, groups,
If(Contains(colname, group),
Insert Into(groups[group], colname);
);
);
);
// Loop over the associative array and create the stacked tables
stacked_tables = {};
For Each({{groupname, groupcols}}, groups,
stacked_dt = dt << Stack(
Columns(Eval(groupcols)),
Source Label Column("Label"),
Stacked Data Column("Data"),
"Non-stacked columns"n(Keep(:tst_temp, :stdf_file)),
Output Table(groupname || " Stacked"),
Invisible(1)
);
Insert Into(stacked_tables, stacked_dt);
);
show(stacked_tables);