你好,
我是腳本編寫新手,非常感謝以下方面的任何幫助:
我正在嘗試編寫一個腳本,該腳本允許我對具有變數名稱後綴的列進行分組和堆疊。 我的表格由一些名稱不變的欄位(即來源、框架)和許多其他欄位(最多數百列)組成,這些欄位以x、y、面積、週長、AR 開頭,並具有後綴1、2 、3 等。所以通常它看起來像這樣...
源、框架、x1、y1、ar1、區域 1、角度 1、週長 1、x2、y2、ar、區域 2、角度 2、週長 2,...等等...
如果有人幫我寫腳本,將所有的 exe、所有的 y 等(無論數量多少)分組,我將非常感激。 之後,我想將它們堆疊在一起,一列表示後綴,另一列表示 x、y、面積、週長的資料。
希望我說清楚了。
我還附上了一張小桌子,以便於理解。
謝謝你的幫助,
安德烈斯
這是一個我認為可以實現您想要的功能的腳本:
Names Default To Here( 1 );
dt = Current Data Table();
// Get all of the column names so we can get a count of the number of
// X and Y columns found
All Columns = dt << get column names;
// Loop through and find the number of Xnn columns & Ynn columns
XList = YList = {};
For( i = 1, i <= N Items( All Columns ), i++,
If( Uppercase( Substr( All Columns[i], 1, 1 ) ) == "X" & Is Missing( Num( Substr( All Columns[i], 2 ) ) ) == 0,
Insert Into( XList, Num( Substr( All Columns[i], 2 ) ) )
);
If( Uppercase( Substr( All Columns[i], 1, 1 ) ) == "Y" & Is Missing( Num( Substr( All Columns[i], 2 ) ) ) == 0,
Insert Into( YList, Num( Substr( All Columns[i], 2 ) ) )
);
);
start = min(XList);
end = max(XList);
// Error checking
If(end!=max(YList) | start!=min(YList) | N items(XList)!=N Items(YList),
dialog("errors in column matching found"); throw());
// Create Column Lists
For(i=1,i<=nItems(XList),i++,
XList[i]=column("X" || char(XList[i]));
YList[i]=column("Y" || char(YList[i]));
);
// Combine the lists
Stack Columns = XList || YList;
// Stack the data
dtStacked =dt << Stack(
columns( Stack Columns ),
Source Label Column( "Label" ),
Stacked Data Column( "Data" ),
Eliminate missing rows( 1 ),
Drop All Other Columns( 1 ),
Number of Series( 2 ),
Contiguous
);
// Create the Suffix column
dtStacked << New Column("Suffix",character,formula(substr(:Label,2)));
// Remove the formula and leave the calculated value
dtStacked:Suffix << delete property("formula");
// Rename the columns, and delete unneeded columns
dtStacked:Data << Set Name("X");
dtStacked:Data 2 << Set Name("Y");
dtStacked << delete columns({"Label","Label 2"});