Conditionally combine columns

Hi, I’m new to Knime and would like to get help in achieving the following:
I have a table with column A and column B. I want to combine them in a new column (via Column combiner or via Rule Engine). The problem I face is that the new column should contain the combined value of Column A and Column B (in that order) if A > B and in case the value of A < B then the combined value of Column B and Column A (in that order). How to achieve this in an efficient way, 1 step possibly? (I have done it with 2 Rule engine nodes + 1 Column Combiner) but it takes 3 steps (one to create RolePair1, another to create RolePair2 and the 3rd to combine the result which is in the RolePairKey column below
image

Hi @jcjaldin and welcome in the KNIME world.
what you call “efficient way” is sometimes using more than one node :slight_smile:
So your solution isn’t bad at all, just close with the Column filter node in order to delete the two temporary columns. For aesthetic reason, you can use the “Collapse into Metanode” option in order to show only one node in your workflow.

To achieve the “one node”-solution you would have to use a Java snippet with…

out_combination = c_col1+"-"+c_col2;
if(c_col2.compareTo(c_col1)<0) out_combination = c_col2+"-"+c_col1;

regards, Tommy

2 Likes

Hi there @jcjaldin,

welcome to KNIME Community!

I agree with @tommy. Sometimes efficient way is using more nodes :slight_smile:

However except from Java Snippet node you can use Column Expressions as a one node solution.

join(max(column(“col1”),column(“col2”)),"-",min(column(“col1”),column(“col2”)))

Br,
Ivan

3 Likes

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