Ottieni nomi di colonne che hanno un valore particolareCIAO,
Ho appena iniziato a usare jmp al lavoro e ho cercato di imparare JSL. Sto cercando di scrivere uno script per ottenere i nomi delle colonne in cui il valore della riga = 1. Ad esempio, la tabella mostra i valori di 0 e 1 in diverse colonne IMCX e voglio scoprire quale colonna ha il valore = 1 in ogni riga e memorizzare sotto una nuova colonna chiamata IMC. Nella prima riga, la colonna IMC 4 ha 1, quindi inserisco manualmente IMC4 sotto IMC. Ci sono più di 5000 righe nelle tabelle. Grazie per l'aiuto.

Automazione e scriptingRe: Ottieni nomi di colonne che hanno un valore particolareCiao Ian,
Passa un po' di tempo a leggere la sezione help>Books e cambia la parola chiave da loc a contains ora funziona. Grazie tonnetto!
// 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: Ottieni nomi di colonne che hanno un valore particolareGrazie per il tuo tempo e aiuto. Non sono stato in grado di eseguire correttamente lo script. Ha creato una colonna IMC ma nessun valore in essa. Farò il debug e cercherò di capire meglio il codice.Re: Ottieni nomi di colonne che hanno un valore particolareCiao Ian, grazie per il tuo tempo e la tua spiegazione. Posso capire il tuo codice. Tuttavia, quando ho eseguito lo script impostando dt come tabella corrente, ho ottenuto empty() nella colonna dell'espressione risultante. Ho molta strada da imparare qui. Grazie a tutti per il vostro contributo e suggerimentiRe: Ottieni nomi di colonne che hanno un valore particolareGrazie per il tuo tempo e aiuto. Mi scuso per non aver esposto chiaramente il problema. In ogni riga, c'è solo un'istanza di 1 sotto una colonna e devo scegliere il nome della colonna e salvare sotto una nuova colonna.Re: Ottieni nomi di colonne che hanno un valore particolareGrazie mille. Ha funzionato!Re: Ottieni nomi di colonne che hanno un valore particolarePer quanto riguarda " Ho appena iniziato a usare jmp al lavoro e ho cercato di imparare JSL "(ed è già stato sottinteso) ci sono molti modi per portare a termine il lavoro.
Quindi, in questo spirito, trova un'altra leggera variazione. Usa "Aiuto > Libri > Guida allo scripting" per capire come funziona.
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: Ottieni nomi di colonne che hanno un valore particolareLa mia soluzione (utilizzando la stessa tabella di dati di esempio di 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 ""
);
);
);Mi piace la prima soluzione @txnelson offerta per due (tre) motivi:
- Sta usando così tante voci di menu trovate direttamente da JMP e potresti Quasi scriverlo senza alcuna conoscenza JSL semplicemente copiando e incollando da fonti datatable
- Sta usando una sintassi simile a JSL con tutti << seleziona dove e simili
- (Penso che il tipo di flusso di lavoro presentato nella soluzione sia molto simile a JMP)
Re: Ottieni nomi di colonne che hanno un valore particolareLa mia lettura dell'input iniziale è che più di 1 colonna su una data riga può avere un valore di 1. In caso contrario, i seguenti 2 script non sono il metodo da utilizzare.
Ecco un modo per farlo
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 );

Ecco un secondo modo per farlo: in questo esempio più di una colonna su una data riga ha valore 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: Ottieni nomi di colonne che hanno un valore particolarePuoi fare qualcosa del genere:

La formula nella colonna Risultato è la seguente:
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 ) )
)
Io uso concatenare parola chiave per controllare ogni colonna per vedere se è un 1.
Fammi sapere se per te funziona!
Ri: Ottieni nomi di colonne che hanno un valore particolareCiao Jthi,
se i miei dati assomigliano a questo esempio ma tutte le colonne sono numeriche (dati continui) e voglio scegliere il nome della colonna tagliato come > 3000 (in 1 riga potrebbe avere più colonne > 3000) cosa dovrei fare sullo script ?
Utilizzo il seguente script
colNames = dt << Ottieni i nomi delle colonne ( "String" );
For (i = N elementi (colNames), i > 3000 , i--,
);
ma non ha funzionato.
Migliore