- Celui-ci parcourt les lignes et les colonnes, en utilisant les noms de colonne pour trouver la réponse
Names Default To Here( 1 );
// Create an Example data table
dt = New Table( "Example 1",
Add Rows( 3 ),
New Column( "Col 1", Character, "Nominal", Set Values( {"Jane", "NA", "NA"} ) ),
New Column( "Col 2", Character, "Nominal", Set Values( {"NA", "Henry", "NA"} ) ),
New Column( "Col 3",
Character,
"Nominal",
Set Values( {"Don", "Terry", "NA"} )
),
New Column( "Col 4",
Character,
"Nominal",
Set Values( {"Tom", "NA", "Jack"} ),
Set Display Width( 48 )
),
New Column( "Col 5", Character, "Nominal", Set Values( {"Amy", "NA", "Bob"} ) )
);
// Get the names of the columns in the data table
colNames = dt << get column names( string );
// Create the new column
dt << New Column( "All Members", character );
// Loop through each row
For( k = 1, k <= N Rows( dt ), k++,
// Loop through all columns finding non NA values
For( i = 1, i <= N Items( colNames ), i++,
If( Column( colNames[i] )[k] != "NA",
If( :All Members[k] == "",
:All Members[k] = Column( colNames[i] )[k],
:All Members[k] = :All Members[k] || ", " || Column( colNames[i] )[k]
)
)
)
);
- Celui-ci parcourt les lignes et les colonnes en utilisant les numéros de colonne pour résoudre le problème
Names Default To Here( 1 );
// Create an Example data table
dt = New Table( "Example 2",
Add Rows( 3 ),
New Column( "Col 1", Character, "Nominal", Set Values( {"Jane", "NA", "NA"} ) ),
New Column( "Col 2", Character, "Nominal", Set Values( {"NA", "Henry", "NA"} ) ),
New Column( "Col 3",
Character,
"Nominal",
Set Values( {"Don", "Terry", "NA"} )
),
New Column( "Col 4",
Character,
"Nominal",
Set Values( {"Tom", "NA", "Jack"} ),
Set Display Width( 48 )
),
New Column( "Col 5", Character, "Nominal", Set Values( {"Amy", "NA", "Bob"} ) )
);
// Get the number of current columns
numCol = N Cols( dt );
// Create the new column
dt << New Column( "All Members", character );
// Loop through each row
For( k = 1, k <= N Rows( dt ), k++,
// Loop through all columns finding non NA values
For( i = 1, i <= numCol, i++,
If( Column( i )[k] != "NA",
If( :All Members[k] == "",
:All Members[k] = Column( i )[k],
:All Members[k] = :All Members[k] || ", " || Column( i )[k]
)
)
)
);
- Celui-ci modifie les valeurs NA dans la table de données en blancs, ce qui est la manière normale d'indiquer les valeurs manquantes pour les colonnes de caractères, puis utilise une fonction intégrée appelée "Combine Columns ()" pour créer la colonne souhaitée
Names Default To Here( 1 );
// Create an Example data table
dt = New Table( "Example 3",
Add Rows( 3 ),
New Column( "Col 1", Character, "Nominal", Set Values( {"Jane", "NA", "NA"} ) ),
New Column( "Col 2", Character, "Nominal", Set Values( {"NA", "Henry", "NA"} ) ),
New Column( "Col 3",
Character,
"Nominal",
Set Values( {"Don", "Terry", "NA"} )
),
New Column( "Col 4",
Character,
"Nominal",
Set Values( {"Tom", "NA", "Jack"} ),
Set Display Width( 48 )
),
New Column( "Col 5", Character, "Nominal", Set Values( {"Amy", "NA", "Bob"} ) )
);
// Get the names of the columns in the data table
colNames = dt << get column names( string );
// Loop through the columns and delete the NA values
For( i = 1, i <= N Cols( dt ), i++,
dt << select where( as column( colNames[i] ) == "NA" );
// set the found cells to a blank
try( column( colNames[i] )[ dt << get selected rows ] = "" )
);
// Create the new column using a built in function
dt << Combine Columns(
delimiter( "," ),
Columns( eval(colNames) ),
Selected Columns are Indicator Columns(0 ),
Column Name( "All Members" )
);
// Move the column to the last position
dt << Move Selected Columns( :All Members, To last );