Skip to content
Advertisement

Filter based on pairs within a group – if value represent at BOTH ends

JavaScript

Within each group there are pairs. In Group 1 for example; the pairs are (2,2),(2,4),(4,1)

I want to filter these pairs based on code numbers 2 AND 4 being present at BOTH ends(not either)

In group 1 for example, only (2,4) will be kept while (2,2) and (4,1) will be filtered out. Excepted Output:

JavaScript

Advertisement

Answer

You can approach by making 2 boolean masks for current row and next row code in 2 or 4. Then, form the required combination condition of present at BOTH ends(not either), as follows:

If you require both 2 AND 4 be present in the pair, then we can make another boolean mask for asserting that these 2 consecutive codes are not equal:

JavaScript

Result:

JavaScript

Another way to do it, may be a little bit simpler for this special case:

JavaScript

Result:

Same result:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement