Add column flags

Hi,

I have a table which will contain an unknown number of columns. For each of those columns, I would like to create an additional column with the suffix _flag. Within the new _flag column, I need to put a true or false depending on whether the original column has a value greater than 0.

I think a column list loop with a rule engine may work but can’t seem to work out the logic. Any help would be amazing!

Hi @tonyjmeade,

combine the column list loop start with a a rule engine.

Edit: I felt compelled to provide my solution as well xD

Best
Mike

3 Likes

Hi @tonyjmeade

See this wf add_column_flags.knwf (34.4 KB)

gr. Hans

2 Likes

Hi @tonyjmeade ,

The Column List loop is useful as shown above, although you do have be careful if you have any additional columns that you don’t want to include in the rules, as they will get appended multiple times into the resultant output unless filtered out on each iteration of the loop and then re-attached afterwards. If your table consists only of the columns that you wish to apply flags to, then the Rule Engine in a loop, as described by @mwiegand and @HansS will do the job perfectly.

Here are some alternative solutions:

Depending on the datatypes of your columns, you could use String Manipulation (Multi-Column) for this.

It will work if your columns are Double or Integer, but unfortunately will fail if they are Long, as String Manipulation (Multi) is bugged when it comes to datatypes other than String, Integer or Double.

But within that restriction, the following code will work (you just need to manually exclude any columns that it cannot handle, and columns that you don’t want to process)

string(
	$$CURRENTCOLUMN$$ > 0?true:false
	)

There is no facility in that node for excluding columns by data type, so that exclusion has to be either performed manually, or by splitting/filtering the columns outside of the node, and bringing them back together afterwards. You can write code to split columns by type, and re-append. If it matters to you, you may also need to add nodes to re-assemble the columns based on their original order in the table.

Something like this:

As a convenience, I have the following two zero-config components that will do the above job for you. These simple filter out any columns other than the ones that the String Manipulation(Multi) node can handle:
image

Ideally though, there would be a Multi-Column Rule Engine node that could this with a syntax such as:

$CURRENTCOLUMN$ > 0 => TRUE
TRUE => FALSE

Unfortunately there isn’t.

… although I do have another component… :wink:

image

What that component does behind the scenes is actually similar in many regards to both of the above (@mwiegand and @HansS ) solutions (except that to make it into a generic component and provide additional “rule engine-based” facilities it contains a myriad of nodes and it was necessary to use a Recursive Loop rather than a Column List Loop)

If they are of use, or interest, the above components are available on the hub here:

EDIT: oops… forgot to attach this, lol:

Multiple flag columns.knwf (240.6 KB)

2 Likes

Thanks Mike, that works exactly as I’d hoped!

1 Like

spratans-prepare-for-glory

Let the best solution win :grimacing:

New version uploaded to the Hub :partying_face:

PS: It is possible to retrieve the column name via columnNames()[i] but I haven’t figured out how to return that for a new column name which would essentially reduce the nodes required to just one. There is also the slight challenge to enforce the output for each column.

Big thanks, as I didn’t managed to express it back then to @AlexanderFillbrunn, in that old post:

2 Likes

I did look into columnNames()[i] and potentially using the JavaSnippet node but couldn’t quite work it out either! On to the next problem :laughing:

1 Like

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