Column expression: Storing values in variable without creating a new column

Dear all,

I new to Knime and still struggling with the node structure. I have a code in my head where I wish to create two new columns, both based on if/else statement. Also I would like to store values beforehand in variables without actually creating them. Also I would like to do that with as few (preferably one) node as possible.
I wanted to use the “Column expression” but I get an error message that "temp_var"is not defined even though i define it in the first row.

my first expression would be

temp_var1 = (column(“A”) - column(“B”))/column(“C”)
temp_var2 = column(“A”)/column(“B”)

if (column(“C”) == null || column(“C”) == 0 || column(“A”) == null || column(“B”) == null) 0

if (temp_var1 < temp_var2) 1

else 0

and the second expression looks similar. I know you can probably do that with java_snippet node but is it also possible with the column expression? Because it should store the values assigned to the temp-var1 and temp_var 2 variables right?

Excuses if a question like this was already asked, I could not find it.

Thanks in advance.

Hi @Francesca

Welcome to the Knime Community!

Your question triggered me to look into this Column Expression node (which I never used before).

It’s possible to add these two columns with just one node (which was a nice surprise to me too).

I’ve constructed a small workflow: 25480.knar (10.4 KB) which looks like this.

The top table is the input, the bottom one is the result of the Column Expression node.

You have to add two expressions in the top half of the configuration, one expression for each column. In the bottom half you define the expression. By clicking on the expression in the top half, you will get its expression in the expression editor.


As you will see I moved the definitions of tempvar1 and tempvar2 to the else-branch, simply to avoid these calculations might go wrong (give an unwanted error) if either one of the columns is missing or the division should be done through zero.
Maybe that’s what caused the error you mention?

The second expression is not so fancy as the above one. It’s just an example… :slight_smile:

Kind regards

2 Likes

Dear @JanDuo,

thank you so much!!

I think the problem was, as you mentioned, that the defintions were not in the right place. I moved them know to the else-branch and it works perfectly.

2 Likes

@Francesca great!

I think I will start using this node more often, because till now I used often a combination of several other nodes to achieve the same result.
So thanks for asking your question as it will help me in the future too :grinning:

Just keep in mind that the node is very slow. No problem if you work with a couple of thousand of rows, but if you want to work somewhat interactively with 100k or millions of row, the waiting gets old very fast.

1 Like

Thanks for this advice.
Which node would you prefer then to solve this problem?

java snippet. In your example your using code anyways so only difference would be that you need to code in Java vs JavaScript (note despite the naming these are very different languages).

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

Info: Column Expressions got speedup with KNIME 4.2.1.