特定の列がデータ テーブルに存在する場合に合計するスクリプトを作成しようとしています。 それらをCol A、Col B、Col C、Col Dと呼びましょう。 問題は、データ テーブルに常に 4 つの列がすべて含まれているとは限らないため、エラーが発生することです。 データ テーブルに存在する列 A、列 B、列 C、および列 D を追加できるスクリプトを作成したいと考えています。 明らかに、毎回存在するコードを更新するだけで済みますが、エラーなしでさまざまなデータ テーブルを処理できる自動化された方法を見つけようとしています。
ありがとう!
こんにちは@DegreesOfGen19 、
これはあなたが探しているものと似ていますか?
Names default to here(1);
dt = current data table();
// your columns of interest
col_list = { "Col A", "Col B", "Col C", "Col D" };
// creates list of column names from the target data table
dt_col_list = dt << Get column names( string );
for( i = 1, i <= N Items( col_list ), i++,
if( contains( dt_col_list, col_list[i] ),
mysum = colsum( column( col_list[i] ) );
show( mysum )
)
);
編集: 変更された dt << Get column names; to dt << Get column names( string );これにより、それぞれ返される列名が文字列になります。これが、ユーザー定義列のリストです。最初にそれをキャッチできなかったのは私の悪いことです。
以下は、あなたができることのいくつかの例です。 見つかった列をフィルター処理する、より単純な (理解しやすい) 見た目の方法がありますが、このタイプの使用例には連想配列が適しています。
Names Default To Here(1);
dt = New Table("Untitled 3",
Add Rows(1),
Compress File When Saved(1),
New Column("Column 1", Numeric, "Continuous", Set Values([1,2,3,.])),
New Column("Column 2", Numeric, "Continuous", Set Values([2,3,4,5])),
New Column("Column 3", Numeric, "Continuous", Set Values([3,4,5,.]))
);
col_list = {"Column 1", "Column 2", "Column 3", "Column 4"};
table_col_list = dt << Get Column Names("String");
// keep only columns which are found from data table
cols_to_keep = Associative Array(col_list);
cols_to_keep << Intersect(Associative Array(table_col_list));
cols_to_keep = cols_to_keep << get keys;
// if you don't need a formula, data table subscripting is quite easy solution
dt << New Column("SumForCols", Numeric, Continuous, << Set Each Value(
Sum(dt[Row(), cols_to_keep]);
));
// if you do need a formula, you can build one with expressions (requires JMP16+ due to For Each usage)
// https://community.jmp.com/t5/JSL-Cookbook/Insert-one-expression-into-another-using-Eval-Insert-Eval-Expr/ta-p/48998
f_expr = Expr(Sum());
For Each({col_name}, cols_to_keep,
Insert Into(f_expr, Name Expr(AsColumn(col_name)));
);
show(name expr(f_expr));
Eval(EvalExpr(
dt << New Column("SumForColsFormula", Numeric, Continuous, Formula(
Expr(name expr(f_expr));
))
));
まず、次を使用して、必要なリストからデータ テーブルに存在する列を計算します連想配列と交差. 次に、 << を使用して各値を設定し、データ テーブルの添え字付け合計または建物の式を計算し、使用するそれか