Possibility to perform math operations

Hi,

I am trying to create a new column which performs or does not perform a mathematical operation based on a second column. Let me give an example

Col1; Col2; NewCol
3; A; 3
3; B; 1

I want to basically do If(Col1 = “A” THen Col1) ElseIf(Col1 = “B” Then Col1 / 3)

I know this is super easy in both Excel and Alteryx, but I have spent 6 hours now and have yet to produce a result like the one above. How can I do this?

Use column Expression node


It supports Java if statement.
if (…) {} else {}
1 Like

If(column(“Col1”) = “B”) {
column(“Col1”)/3;
}
Else {
column(“Col1”);
}

How do I do this? Are there any tutorials for simple stuff?

See example below

Great, thank you! That worked!!

Is there any site I can read up on how to write these expressions, so I don’t have to post every time I run into issues with Java in the Column Expression? For me it was not obvious to use regexmatch here instead of equal operator. Thanks in advance :slight_smile:

Java use == as equal sign.

Hi there @Polestar01,

Welcome to KNIME Community Forum!

If you are a new user I recommend you to start with some tutorials on KNIME. There is Learning section on KNIME home page where you check E-Learning courses, KNIME Youtube channel, documentation and much more. Also there is KNIME Hub where you can search for nodes and example workflows which is a great way to learn KNIME. As you already discovered KNIME Community Forum is a place to ask questions, get help and share you knowledge. Feel free to contribute :wink:

Additionally if you are coming from Alteryx check out this forum topic for more information: Considering switching to KNIME from Alteryx

Now regarding your problem. If you want to use only one node Column Expressions is the node to use. It is based on JavaScript syntax so if you are not familiar with it check out some tutorial for it. I always have a look here for JavaScript help. You do not have to use Regex function for it as your syntax is pretty close. JavaScript is case sensitive so it is if and else without capital letters. Also as @izaychik63 said it is not one equal sing but rather two. So here is a syntax that should work:

if(column("column1") == "B") {
    column("column2")/3;
    }
    else {
    column("column2");
    }

If you do not want to use coding you can check out Rule Engine node. It can not perform arithmetic operations so you should use Math Formula node after it for example. I have created an example workflow so take a look.
2019_05_07_Rule_Engine&Math_Formula_example.knwf (12.0 KB)

Hope this helps!

Br,
Ivan

2 Likes

Hi @Polestar01 -

In addition to the resources @ipazin already listed, you might find this blog post by @armingrudd helpful. It covers syntax in the Column Expressions node for a lot of standard operations:

https://blog.statinfer.com/knime-multitasking-with-column-expressions-node/

2 Likes

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