i´m facing a new challenge in KNIME and want to know if you can help me with the following:
I want to create numbers in column “D” which refers to some rows logic. It should give me “1” if A from column “A” had anything else than “Tree” in column “C” in 2020. The list is much longer and it´s not only 2021 & 2020. In any other cases it should give a “0” in column “D”. And the output should appear only in 2021.
The nodes that can help you do this are either the Rule Engine node or the Column Expressions node. They allow you to define custom transformation rules. Take a look at his example workflow on how to use them:
With the following code, I can create a table like the one you had in your first post:
out = 0
if(column("column2") == "2021" && column("column3") == "Tree"){
out = 1
}
out // this is needed so that the expression returns this as the new value
I want to create “New_Column” depending on rows from “2020”, meaning if “A” has no “Tree” in 2020 then it should show a “1”. As you can see in former output for C it is showing a “0” because it was in 2020 also “Tree”.
That is a bit more involved, I used the pivot and unpivot nodes to get the data into a shape that makes sense for the column expressions node and then to restore the original data layout:
the column expression node looks at each row independently. So you can’t do any logic that spans several rows. You would need to use a node that loads the whole table at once, like the Python Snippet.
Some time later… … The specific case of logic for one row based on another rows can sometimes be resolved by thinking in terms of “sets” rather than specific rows.
Here is a workflow that does just that to try to solve the problem that you have posed in your sample data. It may or may not work for your additional data, but you may be able to adapt the idea to assist you. What I have done is to take your primary data set and create from it a subset of items that did not have a tree in the 2020 row. By then joining back with a “left outer join”, you can infer which items should be “1” and which should be “0”. Some additional logic is added in a “rule engine” to set all items to “0” if they have a year of “2020”.
As I said, this workflow answers the specific question, and may not be sufficiently generic for your needs but it returns the “desired output” that was posted, and the ideas may be adaptable to your further use case.