大家好。我為特定任務製作了此 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);