- これは行と列をループし、列名を使用して答えを導き出します。
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]
)
)
)
);
- これは、列番号を使用して行と列をループして問題を解決します。
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]
)
)
)
);
- これは、データ テーブル内の NA 値を空白に変更します。これは、文字列の欠損値を示す通常の方法であり、その後、「Combine Columns()」と呼ばれる組み込み関数を使用して目的の列を作成します。
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 );