Hola,
Soy nuevo en scripting y agradecería enormemente cualquier ayuda con lo siguiente:
Estoy tratando de escribir un script que me permita agrupar y apilar columnas con sufijos de nombre de variable. Mi tabla consta de algunas columnas con nombres que no cambian (es decir, fuente, marco) y muchas otras (hasta cientos) que comienzan con x, y, área, perímetro, AR y tienen los sufijos 1, 2, 3, etc. así que normalmente se vería así ...
fuente, marco, x1, y1, ar1, área 1, ángulo1, perímetro1, x2, y2, ar, área2, ángulo2, perímetro2, ... y así sucesivamente ...
Apreciaría enormemente cualquier ayuda para armar un guión que agruparía todos los exes, todos los ys, etc., independientemente del número. Después de esto, me gustaría apilarlos con una columna que indica el sufijo y las otras columnas con los datos de x, y, área, perímetro.
Ojalá me haya aclarado.
También estoy adjuntando una mesa pequeña para que sea más fácil de seguir.
Gracias por cualquier ayuda,
Andres
Aquí hay un script que creo que hará lo que quieras:
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"});