自動將列分配給群組我一直在努力弄清楚這一點。我有一個表 (Table1),其中包含我想要分組的列。 Table2 有我想要使用的列名稱和群組標籤。我想編寫腳本並自動建立分組表,如下面的螢幕截圖所示。任何有關如何執行此操作的建議將不勝感激。

以下是建立上面所示的 Table1 和 Table2 的腳本
dt1 = New Table( "Table1",
Add Rows( 3 ),
New Column( "A", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [35, 10, 88] ) ),
New Column( "B", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [2, 3, 2] ) ),
New Column( "C", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [1, 3, 1] ) ),
New Column( "D", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [2, 2, 2] ) )
);
dt2 = New Table( "Table2",
Add Rows( 4 ),
New Column( "Name", Character, "Nominal", Set Values( {"A", "B", "C", "D"} ) ),
New Column( "GroupName", Character, "Nominal", Set Values( {"G1", "G1", "G2", "G2"} ) )
);
自動化和腳本編寫回覆:自動將列分配給群組我使用與 Jim 幾乎相同的想法,但我喜歡將群組收集和創建分離到它們自己的循環中
Names Default To Here(1);
dt1 = New Table("Table1",
Add Rows(3),
New Column("A", Numeric, "Continuous", Format("Best", 12), Set Values([35, 10, 88])),
New Column("B", Numeric, "Continuous", Format("Best", 12), Set Values([2, 3, 2])),
New Column("C", Numeric, "Continuous", Format("Best", 12), Set Values([1, 3, 1])),
New Column("D", Numeric, "Continuous", Format("Best", 12), Set Values([2, 2, 2]))
);
dt2 = New Table("Table2",
Add Rows(4),
New Column("Name", Character, "Nominal", Set Values({"A", "B", "C", "D"})),
New Column("GroupName", Character, "Nominal", Set Values({"G1", "G1", "G2", "G2"}))
);
aa_groups = Associative Array(Column(dt2, "GroupName"));
For Each({key}, aa_groups << get keys,
aa_groups[key] = dt2 << Get Rows Where(:GroupName == key);
);
For Each({{name, cols}}, aa_groups,
dt1 << Group Columns(name, cols);
);回覆:自動將列分配給群組這是處理該問題的一種方法:
Names Default To Here( 1 );
dt1 = New Table( "Table1",
Add Rows( 3 ),
New Column( "A", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [35, 10, 88] ) ),
New Column( "B", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [2, 3, 2] ) ),
New Column( "C", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [1, 3, 1] ) ),
New Column( "D", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [2, 2, 2] ) )
);
dt2 = New Table( "Table2",
Add Rows( 4 ),
New Column( "Name", Character, "Nominal", Set Values( {"A", "B", "C", "D"} ) ),
New Column( "GroupName", Character, "Nominal", Set Values( {"G1", "G1", "G2", "G2"} ) )
);
GroupNames = Associative Array( dt2:GroupName ) << get keys;
For Each( {theGroup}, GroupNames,
// Create a list for a group
theRows = dt2 << get rows where( :GroupName == theGroup );
grpList = {};
For Each( {rowValue}, theRows, Insert Into( grpList, dt2:Name[rowValue] ) );
dt1 << Group Columns( theGroup, grpList );
)
;