dt = Current Data Table();
// List of column names for the table
col_names = dt << get column names( string );
// Create cumulative sum of each D
looking_for_channel_1 = "D";
For( i = 1, i <= N Items( col_names ), i++,
// search column names for "D"
If( Starts With( col_names[i], looking_for_channel_1 ),
new_col = dt << New Column( Char( col_names[i] ) || ">=",
Numeric,
.................................
);
);
);
Estimados,
Soy un nuevo scripter que intenta resolver un problema actual. No estoy muy familiarizado con la sintaxis, pero quiero recorrer columnas y filas para calcular la suma acumulada para cada herramienta y fila y guardar el resultado en una nueva columna. Adjunto una tabla de datos que muestra la solución.
Muchas gracias por cualquier comentario proporcionado.
BR
Martín
Aquí hay otro enfoque, simplemente creando las nuevas columnas usando columnas de fórmula.
Names Default To Here( 1 );
// This JSL assume the table contains only the Tool column,
// followed by the Dnnn columns. No cummulative columns have been
// created yet
dt = Current Data Table();
totCol = N Cols( dt );
// Loop across the columns creating the new cumulative sums
For( i = 2, i <= totCol, i++,
Eval(
Substitute(
Expr(
dt << New Column( __newname__, formula( __formula__ ) )
),
Expr( __newname__ ), Column( dt, i ) << get name || ">=",
Expr( __formula__ ),
If( i == 2,
Parse( Column( dt, i ) << get name ),
Parse(
"sum( :Name(\!"" || char( Column( dt, i - 1 ) << get name ) || ">=\!"), " || char( Column( dt, i ) << get name )
|| ")"
)
)
)
)
);He adoptado un enfoque diferente para resolver su problema.Recorrer los datos y acumularlos como usted propone se volverá bastante lento con muchas columnas y filas.Usar las funciones JMP integradas será mucho más rápido.A continuación se muestra un script que transpone los datos a una tabla de datos alta para realizar el procesamiento, utilizando la función integrada Col Cumulative Sum(), y luego transpone los datos nuevamente al formato ancho una vez finalizado el procesamiento.
names default to here(1);
dt=current data table();
// Transform the data into a long table from the current wide table
// to allow for faster processing using the built in column function
// col cumulative sum()
dtStack = dt << Stack(
columns( :D300, :D200, :D100, :D50, :D10 ),
Source Label Column( "Label" ),
Stacked Data Column( "Data" ),
Name( "Non-stacked columns" )(Keep( :Tool, :Column 13 ))
);
// Add a new column to insure an alphbetical ordering of the original
// data table
dtStack << New Column( "Row Number",
Character,
"Nominal",
Formula(
If( Lag( :Tool ) != :Tool,
counter = 0
);
counter = counter + 1;
result = Substr( "00", Length( Char( counter ) ) - 3 ) || Char( counter )
|| " " || :Label;
)
);
// Add the Cumulative Sum column
dtStack << New Column( "xx",
Numeric,
"Continuous",
Format( "Best", 12 ),
Formula( Col Cumulative Sum( :Data, :Tool ) ),
Set Selected
);
// Transpose the data back into a wide format
dtFinal = dtStack << Split(
Split By( :Row Number ),
Split( :Data, :Name( "xx" ) ),
Group( :Tool ),
Remaining Columns( Drop All ),
Sort by Column Property
);
// Close the nolonger needed stack data table
close(dtStack,nosave);
// Pass across the column names adjusting them back to the desired form
For( i = 2, i <= N Cols( dtFinal ), i++,
If(
Word( 1, Column( dtFinal, i ) << get name, " " ) == "Data",
Column( dtFinal, i ) << set name( Substr( Column( dtFinal, i ) << get name, 10 ) )
,
Word( 1, Column( dtFinal, i ) << get name, " " ) == "xx",
Column( dtFinal, i ) << set name( Substr( Column( dtFinal, i ) << get name, 8 ) || ">=" )
)
);