Column Expressions - IfThenElse: change 1 String value in case of Multiple Number options

I’m new here in Knime. I hope someone can help me on the following unsolved tasks:

  • I have a questionnaire with several questions with multiple (4 to 6) options (String type), and I need to change them into numbers (Integer type; ex: 0; 1; 2; …; 6).
  • I wanted to use Column Expressions node, but I haven’t yet found the right Java syntax to write all the needed code lines for those differnt questions (that have similar answers: I need to change them from string to integer, as above).
    Thanks for any hep.

Hi @rogerius1st

Hopefully the below example illustrates how to accomplish this. You’ll need to make sure you configure the Column Expression node to replace the existing column and change the data type to Number (integer).

if (column("StringOption") == "Option 1") 0
else if (column("StringOption") == "Option 2") 1
else if (column("StringOption") == "Option 3") 2
else if (column("StringOption") == "Option 4") 3
1 Like

@rogerius1st welcome to the KNIME forum. If you just want to replace certain values straight away you might do this. It would be interesting to know if you have some sort of dictionary that you would have to replace or if it is just a simple string value that might be the same over a selection of columns.

hi @rogerius1st ,
multiple questions can be saved in different ways by different questionnaire sw. Could you please upload an example table with the same structure of your dataset (or a subset of it)?

1 Like

Welcome to the forum, @rogerius1st.

It would be best if you shared least some example data, as well your desired output.

It’s possible that the Category to Number node will give you what you want without any coding necessary.

2 Likes

Dear @sforesti,
Your suggestion worked out pretty fine, that multiple options in if-else-statements were exactly my problem; now I can go forward to solve my remainder problems…
Thank you so, SO much.

1 Like

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