다음은 이를 수행하는 한 가지 방법의 예입니다.
Names Default To Here( 1 );
dt =
// Open Data Table: Big Class.jmp
// → Data Table( "Big Class" )
Open( "$SAMPLE_DATA/Big Class.jmp" );
my_func = Function( {input_dt, input_col_list, x},
{input_dt, input_col_list, x, the_cmd},
input_dt << New Column( "d" );
current data table(input_dt);
the_cmd = "current data table():d << set formula( If( current data table():\!"" ||
Char( input_col_list[1] ) || "\!"n >= " || Char( x );
For( i = 2, i <= N Items( input_col_list ), i++,
the_cmd = the_cmd || " | current data table():\!"" || Char( input_col_list[i] ) || "\!"n >= "
|| Char( x )
);
the_cmd = the_cmd || ", 1, 0) );";
Eval( Parse( the_cmd ) );
);
input_col_listx = {"Height", weight};
dtx = Data Table( "big class" );
xx = 101;
theReturn = my_func( dtx, input_col_listx, xx );
문자열을 사용하여 이것을 빌드하는 것은 표현식이 매우 간단하기 때문에 가장 간단한 옵션일 가능성이 높습니다. 하지만... 나는 Eval(Parse())를 사용하는 것을 좋아하지 않으므로 여기에 그것을 사용하지 않는 몇 가지 옵션이 있습니다(두 번째 옵션은 누락된 값으로 원하는 작업을 수행하지 않을 수 있음).
Names Default To Here(1);
dt = New Table("Untitled 3",
Add Rows(0),
Compress File When Saved(1),
New Column("a", Numeric, "Continuous", Format("Best", 12), Set Values([0,1])),
New Column("b", Numeric, "Continuous", Format("Best", 12), Set Values([0,0])),
New Column("c", Numeric, "Continuous", Format("Best", 12), Set Values([0,0]))
);
input_col_list = {"a", "b", "c"};
x = 0.1;
// using Or
or_expr = Expr(Or());
// https://community.jmp.com/t5/JSL-Cookbook-Archived/Insert-one-expression-into-another-using-Eval-Insert-Eval-Expr/ta-p/48998
For Each({col_name}, input_col_list,
col_expr = Substitute(
expr(_col_ref_ >= _comparison_value_),
Expr(_col_ref_), NameExpr(AsColumn(Column(dt, col_name))),
Expr(_comparison_value_), x
);
Insert Into(or_expr, Name Expr(col_expr));
);
// Show(name expr(or_expr));
Eval(EvalExpr(dt << New Column("d", Numeric, Continuous, Formula(
If(Expr(name expr(or_expr)), 1, 0)
))));
// using matrix and any
col_refs = Transform Each({col_name}, input_col_list,
Name Expr(As Column(Column(dt, col_name)));
);
Eval(EvalExpr(dt << New Column("d", Numeric, Continuous, Formula(
Any(Matrix(Expr(col_refs)) >= Expr(x))
))));
// function of second option
create_check_col = function({dt, col_input, limit_val}, {Default Local},
col_refs = Transform Each({col_name}, col_input,
Name Expr(As Column(Column(dt, col_name)));
);
new_col = Eval(EvalExpr(dt << New Column("d", Numeric, Continuous, Formula(
Any(Matrix(Expr(col_refs)) >= Expr(limit_val))
))));
return(new_col);
);
create_check_col(dt, input_col_list, x);주어진 열 이름 목록을 입력으로 사용하고 해당 열을 사용하여 OR 조건을 수행하고 해당 루핑 OR 수식이 있는 열을 출력하는 함수를 jsl에 만들고 싶습니다. 어떻게 합니까?
input_col_list = { "a", "b", "c"};
input_dt = dt;
x = 0.1;
input_dt << New Column("d", numeric, continuous,
formula (
If(
(As Column("a") >= x | As Column("b") >= x | As Column("c") >= x),
1,
0
)
));위의 경우 아래와 같은 기능을 만들고 싶습니다.
my_func =
Function(
{input_dt, input_col_list, x},
{//local var},
// function body
...
Return As column("d")
);