I have a dataset with binary variables of multiple columns. I would like to change “0” to “no” and “1” to “yes” but as the rule engine enables only one column I couldn’t make it. I tried with String Manipulation ( after converting the numbers to string) but it gives me error each time. Please help me, thanks.
Ada
Hi @adaoz and welcome to the KNIME community.
Using String Manipulation (Multi-column), there are a couple of ways this can be achieved.
You can use the replace function nested so that it replaces “0” with “no”, and “1” with “yes” as follows:
replace(
replace($$CURRENTCOLUMN$$,"0","no")
,"1","yes")
This pre-supposes that “0” and “1” are the only values, because if a value were “01” for example, it would return “noyes”!
More precise control could be achieved using regexReplace instead so that a value had to be exactly “0” or exactly “1” (not just containing a zero or one):
regexReplace(
regexReplace($$CURRENTCOLUMN$$,"^0$","no")
,"^1$","yes")
An alternative is to use a conditional to test for “0” or “1” and return the appropriate value
string(
$$CURRENTCOLUMN$$.equals("0")
?"no"
:$$CURRENTCOLUMN$$.equals("1")
?"yes"
:$$CURRENTCOLUMN$$
)
This means return a string such that if the current cell value equals “0”, return “no” otherwise if the value equals “1” return “yes”, otherwise return the current value unchanged
Here is an example
Multi Column string replace.knwf (11.2 KB)
This one worked but I had to convert it from double to int then string. Because in my first trial for 1.0 it gave me yes.no and your other suggestions didn’t work at all for some reason.
Anyways thank you for responding that fast, my problem resolved
Hi @adaoz , my pleasure.
I had read into your initial post that you already had done the conversion to String, and of course this is always the problem when there isn’t sample data to look at as those little things can make all the difference to the approach. Anyway I’m glad you managed to get it working.
There is also a node called Round Double which is a strange name but with there correct settings can achieve the conversion from Double to (integer) String in a single node.
Alternatively within the String Manipulation node you can convert to Int and string using
string(toInt( $$CURRENTCOLUMN$$ ))
But in this case I think doing the conversion prior to String Manipulation as you are doing is more straightforward.
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.