Checking on a column, concatenate values of two other columns

I am new to Knime. I have a data-frame like this:

What I have to do … for a row, if the option_present value is FALSE, then answer value will be concatenated to the question value and removed from the answer column

For example, in the Row 9… question will be concatenated with “Household member” (the ‘answer’)za

Hello @soumyabg,

As per my understanding, a “simple” Column Expressions” will do :
Example : From start to result :

For your dataset the formula should look like this :
if (column(“option_present”)==“False”) {
join(column(“question”),column(“answer”))
} else {column(“question”)}


Ps: You have to check “Replace Column” in the node

Basic Javascript : JavaScript | MDN (mozilla.org)

Br,
Samir

4 Likes

In addition to @SamirAbida’s note,

to clear down the answer column, an additional expression within the same Column Expressions node can be used with a similar expression:

An alternative approach could be to use a less well documented form of expression within the String Manipulation node:
image

image

Conditional String Concatenate and Clear.knwf (10.4 KB)

4 Likes

That’s one of the limitations that I don’t like about Knime - and it cannot do otherwise, in that you have to have different nodes or different expressions when dealing with different output columns, even though you want to apply the same logic, meaning the same if statement has to be repeated, and more importantly, re-evaluated.

And on the maintenance side, if for some reason you have to make modifications to the logic, you have to go to each node or expression to modify the logic. For example, what if in the future they decide that the logic should change from option_present being False to being False or Empty? That’s very likely and possible.

If you want the if statement to be evaluated only once, then you have to use programming languages.

So, as an alternative solution, you can also use a Java Snippet (Python, etc also will do):

if(c_option_present.equals("False")) {
	out_question = c_question + c_answer;
	out_answer = "";
} else {
	out_question = c_question;
	out_answer = c_answer;
}

Input:
image

Output:
image

All done by a centralized if statement.

Here’s the workflow:
Conditional String Concatenate and Clear - using Java Snippet.knwf (7.0 KB)

3 Likes

@bruno29a I agree with you.

Further, I think, one of the major aims of using Knime is to avoid writing adhoc script, instead using a sequence of nodes. But you can see that your, @SamirAbida and @takbb 's solutions - all using custom scripting in some way.

For example, I was thinking of using Column Merger node but it will trigger on a specific condition (False in option_present column. But there is no trigger option in Knime nodes. I do not know if this is possible in some other way.

@soumyabg,

There is always “the other way”, that is the true beauty of using Knime, in my humble opinion.
That being said, doing everything using just node and no code is possible, but quite complicated. I think we have to find the golden mean between the two for ease to use, performance etc…

Br,
Samir

2 Likes

Hi @soumyabg ,

I agree it would be nice to have nodes that are so generic that this kind of task could be achieved without any kind of scripting. Certainly there are some tasks where I could envisage a sequence of nodes that are commonly used together could be potentially replaced by one configurable node to do the same job.

That said, there is such a variety of tasks that people wish to perform that I think we will always have to assume that some level of scripting is going to be required. I favour no-code where possible but I am happy with a low-code version if that’s what it takes to fulfill the job. I tried the challenge of achieving your task with no-code nodes. It can be done, and perhaps the challenge is now for somebody to improve on mine, as it is a lot of nodes… :wink:

anyway… here it is:

I was disappointed I couldn’t find a way of aggregating two columns and replacing the question column with the output using just one node.

I was also disappointed that the Table Manipulator wouldn’t let me rename the “combined_question” column as “question” (because “question” already existed), even though I was also telling it to remove the “question” column, and so I was forced to precede it with a Column Filter.

It’s also a shame that I had to introduce a Counter Generation so that I could ensure rows ended up in the original order, since sorting by RowID sorts by ascending “string value” and not by numeric value.

So there are a lot of (what should be) superfluous nodes here which just seem to add clutter. :thinking:

Conditional String Concatenate and Clear-2.knwf (31.3 KB)

[Edit: corrected table manipulator as somewhere it had lost config to move “combined_question” to the beginning of the columns]

3 Likes

@takbb I’m super-impressed with your no-code solution. Your disappointments are all legit :slight_smile: .

2 Likes

Thanks @soumyabg ,

I guess a half-way house solution which retains one low-code-node but makes it a low-low-code-node :wink: might be this:

so the String Manipulation contains only the following:
image
and means that the Column Aggregator, Column Filter and Table Manipulator can all be removed, as it performs the concatenation into the required destination column (a shame Column Aggregator doesn’t have such an option).

This at least satisfies the very valid point made by @bruno29a that we aren’t repeating logic across two nodes, and the level of scripting is a minimum.

Conditional String Concatenate and Clear-3.knwf (42.7 KB)

3 Likes

Hi @soumyabg , another way for doing this with no script is to use an IF Switch. It’s basically very much similar to @takbb 's approach, but you don’t have to separate and concatenate the data where you require to re-sort (of course, this is also tackled in @takbb 's workflow).

The only thing with IF Switch is that it cannot process the PortChoice from the table, and for that, we have to use a loop so that the PortChoice can change depending on the data.

This is what the workflow looks like with the IF Switch:

Same expected results as before:
image

And here’s the workflow:
Conditional String Concatenate and Clear - no script.knwf (28.3 KB)

As you can see in all 3 no-script solutions, it’s kind of longer, more nodes involved, and you can’t get out of the fact that if you are dealing with 2 different columns, you need to have separate nodes for them. With scripting, it’s a bit more direct, but the downside is that you need to be able to do scripting.

EDIT: While we still needed 2 String Manipulation to handle the changes in columns question and answer, this workflow centralized the logic in the Rule Engine node. So if the logic changes in the future, there’s only 1 place to change it. Similarly with @takbb , it’s centralized in the Row Splitter node in his workflow.

3 Likes

@takbb,

So basically, you beat yourself in this challenge :wink:, right ?
TBH, I tried but didn’t find a shorter solution (with no code).

@bruno29a, @takbb thank you for the time you take to always explain things in a simple and concise way.

Looking forward to reading you.
Br,
Samir

1 Like

hehe @SamirAbida , well, I am the only person I can beat in a challenge! :wink: And even then I have to cheat a little by using a “code node”! :rofl:

3 Likes

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