大家好。我为特定任务制作了此 JSL 函数,但我想知道是否有更好的方法来执行此操作。
我想要的是一种方法,如果您有一个索引向量,则创建指定链接的指示向量,其中向量元素的值对于指定索引为 1,否则为 0。例如
[2, 3, 6] --> 转换为长度为 10 的指示向量。
[0, 1, 1, 0, 0, 0, 1, 0, 0, 0]
用例是根据多个条件在数据表中选择行的子集,对于每个条件,我得到一个满足条件的行号向量,我想得到满足所有条件的行。
// Loc Inverse Function
// This function converts a vector of positive integers (locvals) into an
// indicator vector of a specified length, n, with the values
// v[I] = 1 if I is in locvals, and 0 otherwise
// example
// Loc Inverse(6, [1,3])
// returns
// [1, 0, 1, 0, 0, 0]
*/
Loc Inverse = Function( {n, locvals},
v = J( n, 1, 0 );
v[locvals] = 1;
v;
);
因此,对于用例,例如,如果我有三个条件,我会得到满足每个条件(v1、v2、v3)的向量中的行号,所以我会这样做
dt=current data table();
Loc Inverse = Function( {n, locvals},
v = J( n, 1, 0 );
v[locvals] = 1;
v;
);
// ...
// code to get v1, v2, v3
// ...
rowindicator1=Loc Inverse(n rows(dt), v1);
rowindicator2=Loc Inverse(n rows(dt), v2);
rowindicator3=Loc Inverse(n rows(dt), v3);
meet_all_conditions_indicator = rowindicator1 & rowindicator2 & rowindicator3;
rows_that_meet_all_conditions = Loc(meet_all_conditions_indicator);