如何將列作為函數的參數傳遞我正在嘗試創建一個函數來創建新列並設定公式:
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("column_1"), Column("column_2"));
自動化和腳本編寫回覆:如何將列作為函數的參數傳遞這是處理這個問題的一種方法

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" );回覆:如何將列作為函數的參數傳遞如果您喜歡使用列(又稱:age)而不是字串(“age”)來引用列,則有兩個問題需要解決:
- 手動產生的函數評估其參數。
因此,如果您使用列作為函數參數,Jmp 將對其求值(即傳回目前行的值 *)
→預防措施:
用Name Expr()包裹列
- 在大多數情況下,Jmp 不會用變數的值(對應的欄位)取代變數(如「col1」),
例如在 new column() 或 << 設定公式中
→強制 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
回覆:如何將列作為函數的參數傳遞非常感謝,正是我需要的。而且 elseif 也好多了!感謝分享
回覆:如何將列作為函數的參數傳遞我總是傳入帶有名稱(和表引用)的列。如果您需要公式,請使用 Eval Insert、Eval Expr、Parse 和 Substitute 方法將一個表達式插入到另一個表達式中來建構它(我更喜歡Eval(EvalExpr()),但在您的情況下,我會使用Eval(Substitute())。
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");