関数の引数として列を渡す方法新しい列を作成し、数式を設定する関数を作成しようとしています。
my_function = Function( {name, col1, col2},
dt << New Column( name, Character);
Column(dt,name) << Set Formula(
If(
:col1 == "Positive" & :col2 ==
"TRUE",
"True Positive",
If(
:col1 == "Negative" & :col2== "TRUE",
"False Positive",
If(
:col1 == "Negative" & :col2 == "FALSE",
"True Negative",
If(
:col1 == "Positive" & :col2 == "FALSE",
"False Negative",
"?"
)
)
)
)
);
);
しかし、作成された数式を見ると、その背後にある文字列ではなく、col1 が渡されています。
関数を次のように呼び出します:
my_function("新しい列", 列("column_1"), 列("column_2"));
自動化とスクリプトRe: 関数の引数として列を渡す方法これに対処する一つの方法は次のとおりです

Names Default To Here( 1 );
dt = New Table( "Example",
Add Rows( 5 ),
New Column( "a",
Character,
"Nominal",
Set Values( {"Positive", "Positive", "Negative", "Negative", "unknown"} )
),
New Column( "b",
Character,
"Nominal",
Set Values( {"TRUE", "FALSE", "TRUE", "FALSE", ""} )
)
);
my_function = Function( {name, col1, col2},
dt << New Column( name, Character );
Eval(
Eval Expr(
Column( dt, name ) << Set Formula(
If(
as Column( Expr( col1 ) ) == "Positive" & as Column( Expr( col2 ) )
== "TRUE", "True Positive",
as Column( Expr( col1 ) ) == "Negative" & as Column( Expr( col2 ) )
== "TRUE", "False Positive",
as Column( Expr( col1 ) ) == "Negative" & as Column( Expr( col2 ) )
== "FALSE", "True Negative",
as Column( Expr( col1 ) ) == "Positive" & as Column( Expr( col2 ) )
== "FALSE", "False Negative",
"?"
)
)
)
);
);
my_function( "new column", "a", "b" );Re: 関数の引数として列を渡す方法列を参照するために文字列 ("age") ではなく列 (別名 :age) を使用する場合は、解決すべき問題が 2 つあります。
- 手動で生成された関数は引数を評価します。
したがって、列を関数の引数として使用すると、Jmpはそれを評価します(つまり、現在の行の値を返します*)
→それを防ぐには:
列をName Expr()で囲む
- ほとんどの場合、Jmpは変数(「col1」など)をその値(それぞれの列)に置き換えません。
例えば、new column() または << set formula
→ Jmp に強制的に実行させるには:
残りの部分を評価する前に、 Eval(Eval Expr( .... Expr( xyz ) ... )を使用して xyz を評価します。
2b) Jmpが列参照を過剰に評価するのを防ぐには(*のように)、 Name Expr()を再度使用します。
したがって、組み合わせて使用する
Expr(名前Expr(colX))
...列を使用したい場所ならどこでも
合計で:
Names Default To Here(1);
dt = Open("$SAMPLE_DATA/Big Class.jmp");
my_function = Function({name, col1, col2},
new_col = Eval(Eval Expr(New Column(name, Character, Formula(Expr(Name Expr(col1)) /* #2b */ - Expr(Name Expr(col2))/* #2b */ )))); // #2
);
col = my_function("diff", Name Expr(:height), Name Expr(:weight)); //#1
Re: 関数の引数として列を渡す方法ありがとうございます。まさに私が求めていたものです。elseif もずっと良くなりました。共有してくれてありがとう
Re: 関数の引数として列を渡す方法私は常に名前(およびテーブル参照)付きの列を渡します。そして、数式が必要な場合は、Eval Insert、Eval Expr、Parse、およびSubstituteメソッドを使用して、1つの式を別の式に挿入して構築します(私はEval(EvalExpr())を好みますが、あなたの場合はEval(Substitute()を使用します)。これを処理する方法の1つのオプションは次のとおりです。
Names Default To Here(1);
dt = Open("$SAMPLE_DATA/Big Class.jmp");
my_function = Function({dt, name, col1, col2},
new_col = Eval(Substitute(
Expr(dt << New Column(name, Character, Formula(
If(_col1_ == "Positive" & _col2_ == "TRUE",
"True Positive",
If(_col1_ == "Negative" & _col2_== "TRUE",
"False Positive",
If(_col1_ == "Negative" & _col2_ == "FALSE",
"True Negative",
If(_col1_== "Positive" & _col2_ == "FALSE",
"False Negative",
"?"
)
)
)
)
))),
Expr(_col1_), Name Expr(AsColumn(dt, col1)),
Expr(_col2_), Name Expr(AsColumn(dt, col2))
));
return(new_col)
);
col = my_function(dt, "test", "name", "sex");
また、JMPにはelseifを使用するオプションがあります(記述されていませんが)。複数のif文をネストする場合は、代わりにそれが機能する可能性があります。
もっと見る...
Names Default To Here(1);
dt = Open("$SAMPLE_DATA/Big Class.jmp");
my_function = Function({dt, name, col1, col2},
new_col = Eval(Substitute(
Expr(dt << New Column(name, Character, Formula(
If(_col1_ == "Positive" & _col2_ == "TRUE",
"True Positive",
_col1_ == "Negative" & _col2_== "TRUE",
"False Positive",
_col1_ == "Negative" & _col2_ == "FALSE",
"True Negative",
_col1_== "Positive" & _col2_ == "FALSE",
"False Negative"
,
"?"
),
))),
Expr(_col1_), Name Expr(AsColumn(dt, col1)),
Expr(_col2_), Name Expr(AsColumn(dt, col2))
));
return(new_col)
);
col = my_function(dt, "test", "name", "sex");