如何将列作为函数的参数传递我正在尝试创建一个创建新列并设置公式的函数:
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”)来引用列,则需要解决 2 个问题:
- 手动生成的函数评估它的参数。
因此如果你使用某一列作为函数参数,Jmp 将对其进行评估(即返回当前行的值*)
→预防方法:
使用Name Expr()包装列
- 在大多数情况下,Jmp 不会用变量的值(相应的列)替换变量(如“col1”)。
例如在新列()或<<设置公式中
→强制 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");