If you need a formula it can get a bit slow but using As Constant can help. You could take a look into Distance(), KDTable() and maybe VPTree().
These are not optimized but are some simple examples on how you can use As Constant() and KDTable/Distance
Current Data Table() << New Column("R", Numeric, Ordinal, Formula(
As Constant(
tree = KDTable(Current Data Table()[0, {"X", "Y"}]);
{rows, dist} = tree << K nearest rows(25, 13);
v = rows[Loc(dist <= 2)];
);
If(Contains(v, Row()),
1,
0
);
));
One example using Distance
Current Data Table() << New Column("R", Numeric, Ordinal, Formula(
As Constant(
five_idx = Where(:Value == 5);
m = :X[five_idx] || :Y[five_idx];
);
Sqrt(Distance(Matrix({:X, :Y})`, m)) <= 2;
));
If you do not need formula, these can be made more efficient
-Jarmo