What is the "value in (a,b,c)" equivalent formula in Knime for column expression node

What is the “value in (a,b,c)” equivalent formula in Knime for column expression node

sample of what i am looking for - if Column X = Values A, or Value B, or Value C, then 1 else 0

Hello @vishnudarsan ,

Instead of using the Column Expression, you can use the Rule Engine node and write expressions like:

" $COL-1$ LIKE “A”=>1
$COL-1$ LIKE “B” =>1
$COL-1$ LIKE “C” => 1
TRUE=>0 "

Output:

Screenshot 2024-01-18 141444

2 Likes

Hi @vishnudarsan. Welcome to the KNIME community

Column Expressions is based on javascript and you could use this construct:

["cat", "dog", "mouse"].indexOf(column("keywords")) != -1 ? 1 : 0;

which would return 1 if the “keywords” column contained cat, dog or mouse, and zero otherwise.

It also works for numeric values

[10, 8, 6].indexOf(column("somenum")) != -1 ? 1 : 0;

alternatively, you can return true or false using this:

[10, 8, 6].indexOf(column("somenum")) > -1

3 Likes

Thank you this works

1 Like

Hello @vishnudarsan,

there isn’t really in() function in Column Expressions node. There is contains() but it accepts only 1 input string. But there is IN operator in Rule Engine node(s) so you can use it instead of adding multiple lines.

Welcome to KNIME Community!

Br,
Ivan

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.