There is a new KNIME forum. You can still browse and read content from our old forum but if you want to create new posts or join ongoing discussions, please visit our new KNIME forum: https://forum.knime.com

Simple loop on columns

Member for

9 years 3 months Alberto

I have a csv file with n columns and would like to execute a loop over the columns.

For i, from 1 to n-1 :
     write a csv file named *i.csv with one colomn = Colum i + Column n

I didn't find anything with Loop Support.

Thanks for yout help...

Comments
Sat, 07/16/2011 - 01:09

Member for

9 years 10 months

richards99

I'm not totally clear what you are trying to do, but I will take a guess below is what you need.

You have a list of columns, and you want to take each column in turn (column n) and combine this column contents with the contents of column i which is fixed. Then you want to write this combined column out to a CSV file.

If thats the case, then read on..

First make sure you have KNIME 2.4 installed.

In Loop Support there is an option to loop over columns, its called "Column List Loop Start" and you need to finish the loop with a "Loop End (Column Append)".

Select all the columns in the include list, except column i which should be in the exclude list, in the "Column List Loop Start" node.

Then attach a "Column Combiner" node, in the configuration Add All columns. Note the location of the column n (the column to be varied) in the include list, i.e. is it 1st or second in the list. Then go to the flow variables tab, open up columns and from either 0 or 1 (whether the column to vary is 1st or 2nd in the list), select "CurrentColumnName".

Now add a "Column Filter" node, add the Combined Column ONLY to the include list and choose ENFORCE INCLUSION. This makes sure all other columns are removed. Now Attach the CSV Writer node.

Now need to create a filename with .CSV in the name. So after the Column Combiner node add a "Rule Engine" node, in here choose Column n in the default label box, and tick that it is a column reference. Click ok. Attach a second "rule engine" node and in the default label box type in .CSV

Now attach a Column Combiner and choose these new columns generated by the Rule Engine nodes. In the config, remove any delimiter and choose no quotes, call the column "File". Outcome from this will give the filename as *.CSV. Now use "TableRow To Variable" node to convert this filename into a variable.

Now add a "Create Temp Dir" node to the workflow, attach the out variable port of the column filter to the in variable port of "Create Temp Dir" node. In the config, add an additional variable path, give the variable a name, i.e. "filename", and for the filename we are going to use the "File" variable, so go to FlowVariables tab, and in the variable_name_pairs/filename section select "File" next to filename. Now click okay. Attach the variable outport of this "Create Temp Dir" node to the variable inport of the "CSV Writer" node.

In the CSV Writer node, enter any character in the output name i.e. 1, now goto flow variables tab, and next to filename, choose "filename" variable from the list, or whatever you called it in the last step.

Now attach a "Loop End (Column Append)" to the Column Filter node to provide a complete loop. That should be it.

Files
Mon, 07/18/2011 - 03:21

Member for

9 years 3 months

Alberto

Hi Richards99,

I think you understood my problem.

Then I tried to do exactly what you said but there is some problems that do not work as you said. The problem seems to come from CSV Node and the adress :

ERROR     CSV Writer     Execute failed: C:\Users\Alberto\AppData\Local\Temp\teste56d8a9a-d3ce-4575-b112-b0f88b5523ef\"2";"8" $Col1$.csv (La syntaxe du nom de fichier, de répertoire ou de volume est incorrecte)

Mon, 07/18/2011 - 05:12

Member for

9 years 10 months

richards99

Hi,

Since I am seeing the bit saying $Col1$.csv, it looks like its all nearly correct. However in the Rule Engine node where you select the current column value, I believe you may have forgotton to put a TICK in the box which says "Use as a Column Reference". The tickbox is just underneath the default label box at the top of the configuration window. Let me know if that does the trick.

Thanks

Simon.

Mon, 07/18/2011 - 05:42

Member for

9 years 3 months

Alberto

Yes indeed. I did not check the tick, because if I do so, I have the Red Light and the message :

Rule Engine     Column references in default label must be enclosed in $

Look at this image.

 

EDIT !!

I know what was destroying everything !
There is a space by default before the first $, so I had to remove it.

Sun, 11/06/2011 - 01:03

Member for

13 years 8 months

wiswedel

I've uploaded a slightly different solution to the public workflow server; the name of the flow is 011_FlowVarsAndLoops/011005_loopColumnsWriteCSV.

(A customer asked a similar question so we created that workflow and sent it to him.)

Fri, 04/10/2015 - 06:38

Member for

8 years 11 months

Gabriel_Cornejo

Hello Wiswedel:

I was reviewing your solution in the public repository workflows flow called KNIME: // EXAMPLES / 011_FlowVarsAndLoops, and I have a question regarding KNIME: // EXAMPLES / 011_FlowVarsAndLoops / 011003_loopParametersKMeans. Can you tell me how the outcome of the loop is interpreted? Database that is used, is the famous IRIS, in which there are three kinds of flowers. It is assumed that this loop, I should say that the best configuration of clusters is 3? If so, what is the criteria to be considered?

Thanks in advance.

Gabriel