Hi all. I made this JSL function for a specific task, but I was wondering if there is a better way to do this.
What I want is a way is if you have a vector of indices, to make an indicator vector of a specified link where the value of the vector element is 1 for the specified indice and 0 otherwise. E.G.
[2, 3, 6] --> convert to indicator vector of length 10.
[0, 1, 1, 0, 0, 0, 1, 0, 0, 0]
The use case is for selecting subsets of rows in a data table based on multiple conditions, where for each condition I get a vector of the row numbers that meet the condition, and I want to get the rows that meet all of the conditions.
// 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;
);
So for the use case, for example, if I have three conditions, I get the row numbers in vectors that meet each condition (v1, v2, v3), so I would do this
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);