JavaScript Language in Column Expression Node

Hello,
I just started to learn Knime and am doing my first steps with data manipulation. In the Column Expression Node I want to add a new output column to a table. For the content I want to do an “if…else statement”:

  1. existing column Gender has either “M” or “F” as a value
  2. I want to add a new column Gender2 with the value “male” or “female” depending on the value of Gender
  3. whatever I try, either Gender2 is filled with only “male” or “female” as a value

a = column(“Gender”)
b = " "

if (a = “F”)
b = “female”
else
b = “male”

I based the above code on one of the learning tutorials … but somehow it does not work as I want it. Please point me to my error. Thank you, Udo

Hello @uhohlfeld,

and welcome to KNIME Community!

In JavaScript operator for comparison is not one equal sign but two ==. One equal sign is assignment operator and that is reason why new column is always filled either with male or female cause if() is always true. Check this workflow example for simple If Else constructions with Column Expressions node:

Additionally here is decent JS tutorial/documentation site I frequently use/consult when dealing with it:

https://www.w3schools.com/js/default.asp

Note that your task can also be easily solved by using Rule Engine node. (I would use that node.)

Hope this helps and in case of any questions don’t hesitate to ask.

Br,
Ivan

2 Likes

Thank you Ivan,
I appreciate your answer and clarification.
This is helpful and gives me even more input for
finding solutions to newbie’s problems on my own :slight_smile:
Best wishes, Udo

2 Likes

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