Obtener nombres de columnas que tengan un valor particularHola,
Empecé a usar jmp en el trabajo y a intentar aprender JSL. Estoy tratando de escribir un script para obtener los nombres de las columnas donde el valor de la fila = 1. Por ejemplo, la tabla muestra valores de 0 y 1 en diferentes columnas IMCX y quiero saber qué columna tiene el valor = 1 en cada fila y almacenar en una nueva columna llamada IMC. En la primera fila, la columna IMC 4 tiene 1, así que ingreso manualmente IMC4 en IMC. Hay más de 5000 filas en las tablas. Gracias por la ayuda.

Automatización y secuencias de comandosRe: Obtener nombres de columnas que tienen un valor particularHola, Ian,
Dedique algún tiempo a leer la sección de ayuda>Libros y cambie la palabra clave de loc a contiene trabajado ahora. ¡Gracias tonelada!
// Get a list of the column names
colList = dt << getColumnNames( "String" );
// Get the '0's and '1's into a matrix
mat = dt << getAsMatrix;
// Value to search for
searchVal = 1;
// Add an 'expression' column and loop rowise . . .
newCol = dt << New Column( "Value " || Char( searchVal ) || " found in Column:", Expression );
for( r = 1, r <= N Row( dt ), r++,
newCol[r] = colList[Contains( mat[r, 0], searchVal )]
);Re: Obtener nombres de columnas que tienen un valor particularGracias por su tiempo y ayuda. No pude ejecutar el script con éxito. Creó una columna IMC pero no tiene valor. Voy a depurar y tratar de entender mejor el código.Re: Obtener nombres de columnas que tienen un valor particularHola Ian, Gracias por tu tiempo y explicación. Puedo entender tu código. Sin embargo, cuando ejecuté el script configurando dt como tabla actual, obtuve vacío() en la columna de expresión resultante. Tengo un largo camino por aprender aquí. Gracias a todos por sus aportes y sugerenciasRe: Obtener nombres de columnas que tienen un valor particularGracias por su tiempo y ayuda. Siento no haber expuesto el problema claramente. En cada fila, solo hay una instancia de 1 debajo de una columna y necesito seleccionar el nombre de la columna y guardarlo en una nueva columna.Re: Obtener nombres de columnas que tienen un valor particularMuchas gracias. ¡Funcionó!Re: Obtener nombres de columnas que tienen un valor particularAcerca de " Empecé a usar jmp en el trabajo y a intentar aprender JSL. "(y ya se ha dado a entender) hay muchas maneras de hacer el trabajo.
Entonces, en este espíritu, encuentre otra ligera variación. Utilice 'Ayuda > Libros > Guía de secuencias de comandos' para averiguar cómo funciona.
Names Default To Here( 1 );
// Create a sample data table
dt = New Table( "Example",
Add Rows( 2 ),
New Column( "IMCO", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [0, 0] ) ),
New Column( "IMC1", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [1, 0] ) ),
New Column( "IIMC2", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [0, 0] ) ),
New Column( "IMC3", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [0, 1] ) ),
New Column( "IMC4", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [1, 0] ) ),
New Column( "IMC5", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [0, 0] ) ),
New Column( "IMC6", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [0, 1] ), Set Display Width( 46 ) ),
New Column( "IMC7", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [0, 0] ) ),
New Column( "IMC8", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [0, 0] ) ),
New Column( "IMC9", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [0, 0] ) ),
New Column( "IMC10", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [0, 0] ) ),
New Column( "IMC11", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [0, 0] ) ),
);
// Get a list of the column names
colList = dt << getColumnNames("String");
// Get the '0's and '1's into a matrix
mat = dt << getAsMatrix;
// Value to search for
searchVal = 1;
// Add an 'expression' column and loop rowise . . .
newCol = dt << NewColumn("Value "||Char(searchVal)||" found in Column:", Expression);
for (r=1, r<=NRow(dt), r++,
newCol[r] = colList[Loc(mat[r, 0], searchVal)];
);
Re: Obtener nombres de columnas que tienen un valor particularMi solución (usando la misma tabla de datos de muestra que txnelson):
Names Default To Here( 1 );
// Create a sample data table
dt = New Table( "Example",
Add Rows( 2 ),
New Column( "IMCO", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [0, 0] ) ),
New Column( "IMC1", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [0, 0] ) ),
New Column( "IIMC2", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [0, 0] ) ),
New Column( "IMC3", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [0, 0] ) ),
New Column( "IMC4", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [1, 0] ) ),
New Column( "IMC5", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [0, 0] ) ),
New Column( "IMC6", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [0, 1] ), Set Display Width( 46 ) ),
New Column( "IMC7", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [0, 0] ) ),
New Column( "IMC8", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [0, 0] ) ),
New Column( "IMC9", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [0, 0] ) ),
New Column( "IMC10", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [0, 0] ) ),
New Column( "IMC11", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [0, 0] ) ),
New Column( "RESIDUAL", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [32.3201037, 20.54947] ) ),
New Column( "TEST", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [., .] ) )
);
//get wanted columns. Assumes that IMC columns start from column 1 and all IMC columns are one after another
colNames = dt << Get Column Names("String");
For(i = N Items(colNames), i > 0 , i--,
If(!Starts With(colNames[i], "IMC"),
Remove From(colNames, i),
break(); //we can break on first occurance
);
);
//new column with IMC with value 1
dt << New Column("IMC", Character,
<< Set Each Value(
rowValues = dt[Row(), 0][1::N Items(colNames)]; //get values of row as matrix
colIndex = Contains(rowValues, 1); //get index of
If(colIndex > 0, //handle
colNames[colIndex], //get column name with the index
""; //when no 1 found use ""
);
);
);me gusta la primera solucion @txnelson ofrecido por dos (tres) razones:
- Está utilizando tantos elementos de menú que se encuentran directamente en JMP y podría casi ingréselo sin ningún conocimiento de JSL simplemente copiando y pegando desde Datatable Sources
- Está usando una sintaxis muy similar a JSL con todos << seleccione dónde está y tal
- (Creo que ese tipo de flujo de trabajo presentado en la solución es muy parecido a JMP)
Re: Obtener nombres de columnas que tienen un valor particularMi lectura de la entrada inicial es que más de 1 columna en una fila dada puede tener un valor de 1. Si ese no es el caso, los siguientes 2 scripts no son el método a usar.
Aquí hay una manera de hacer esto
Names Default To Here( 1 );
// Create a sample data table
dt = New Table( "Example",
Add Rows( 2 ),
New Column( "IMCO", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [0, 0] ) ),
New Column( "IMC1", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [0, 0] ) ),
New Column( "IIMC2", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [0, 0] ) ),
New Column( "IMC3", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [0, 0] ) ),
New Column( "IMC4", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [1, 0] ) ),
New Column( "IMC5", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [0, 0] ) ),
New Column( "IMC6", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [0, 1] ), Set Display Width( 46 ) ),
New Column( "IMC7", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [0, 0] ) ),
New Column( "IMC8", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [0, 0] ) ),
New Column( "IMC9", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [0, 0] ) ),
New Column( "IMC10", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [0, 0] ) ),
New Column( "IMC11", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [0, 0] ) ),
New Column( "RESIDUAL", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [32.3201037, 20.54947] ) ),
New Column( "TEST", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [., .] ) )
);
// Generate a list of columns to be processed
colNamesList = dt << get column names(string);
remove from (colNamesList,13,2);
// Find the maximum( the ones ) for the columns in question
dtSum = dt<< Summary(
Max(colNamesList),
Freq( "None" ),
Weight( "None" ),
statistics column name format( "column" ),
Link to original data table( 0 )
);
// Stack all of the summarized columns
dtStack = dtSum << Stack(
columns(
colNamesList
),
Source Label Column( "IMC" ),
Stacked Data Column( "Data" )
);
close( dtSum, nosave );
// Delete all data that do not have 1
dtStack << select where(:Data !=1 );
dtStack << delete rows;
// Delete the unwanted columns from the summary table
dtStack << delete columns( {N Rows, data} );
// If there are more rows than in the original table, add rows
if( n rows( dt ) < N Rows( dtStack ),
dt << add rows( N Rows( dtStack ) - N Rows( dt ) )
);
// Add the column names for the columns found with 1's in their data
dt << Update( With( dtStack ) );
close( dtStack, nosave );

Aquí hay una segunda forma de hacerlo: en este ejemplo, más de una columna en una fila dada tiene un valor de 1
Names Default To Here( 1 );
// Create a sample data table
dt = New Table( "Example",
Add Rows( 2 ),
New Column( "IMCO", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [0, 0] ) ),
New Column( "IMC1", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [1, 0] ) ),
New Column( "IIMC2", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [0, 0] ) ),
New Column( "IMC3", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [0, 1] ) ),
New Column( "IMC4", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [1, 0] ) ),
New Column( "IMC5", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [0, 0] ) ),
New Column( "IMC6", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [0, 1] ), Set Display Width( 46 ) ),
New Column( "IMC7", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [0, 0] ) ),
New Column( "IMC8", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [0, 0] ) ),
New Column( "IMC9", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [0, 0] ) ),
New Column( "IMC10", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [0, 0] ) ),
New Column( "IMC11", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [0, 0] ) ),
New Column( "RESIDUAL", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [32.3201037, 20.54947] ) ),
New Column( "TEST", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [., .] ) ),
New Column( "IMC", Character, "Nominal", Set Values( {"IMC1", "IMC3"} ) )
);
// Generate a list of columns to be processed
colNamesList = dt << get column names( string );
Remove From( colNamesList, 13, 2 );
foundNamesList = {};
For( i = 1, i <= N Items( colNamesList ), i++,
theRows = dt << get rows where( As Column( colNamesList[i] ) == 1 );
If( N Rows( theRows ) > 0,
Insert Into( foundNamesList, colNamesList[i] )
);
);
dt << New Column( "IMC", character );
// If there are more rows than in the original table, add rows
If( N Rows( dt ) < N Items( foundNamesList ),
dt << add rows( N Items( foundNamesList ) - N Rows( dt ) )
);
If( N Items( foundNamesList ) > 0,
dt:IMC << set values( foundNamesList )
);

Re: Obtener nombres de columnas que tienen un valor particularPuedes hacer algo como esto:

La fórmula en la columna Resultado es la siguiente:
If( :A == 0,
"",
Char( Column Name( 1 ) )
) || If( :B == 0,
"",
Char( Column Name( 2 ) )
) || If( :C == 0,
"",
Char( Column Name( 3 ) )
) || If(
== 0,
"",
Char( Column Name( 4 ) )
)
yo suelo concatenar palabra clave para verificar cada columna para ver si es un 1.
¡Avísame si te funciona!
Re: Obtener nombres de columnas que tengan un valor particularHola jthi,
Si mis datos se parecen a este ejemplo pero todas las columnas son numéricas (datos continuos) y quiero elegir el nombre de la columna con un corte como > 3000 (en 1 fila puede haber varias columnas > 3000), ¿qué debo hacer en el script? ?
Yo uso el siguiente script
colNames = dt << Obtener nombres de columnas ( "Cadena" );
Para (i = N elementos (colNames), i > 3000 , i--,
);
Pero no funcionó.
Mejor