Loop on rows with cycles...

Hi,

I have 2 columns “LUCE” and  “LUCENum” that represent the light cycle (a cycle always start with one ON, then there are random numbers of LIGHT, and always finish with one OFF).

LUCE LUCENum

ON         1

LIGHT    2

LIGHT    2

OFF        3

ON         1

LIGHT    2

LIGHT    2

LIGHT    2

LIGHT    2

OFF        3

ON         1

LIGHT    2

OFF        3

(Where 1 =ON;      2 = LIGHT;     3 = OFF).

Using Loops I would like to create a new column called RESULT with an ID for each cycle. In other words the column Result will contain a count that increase each time LUCENum  =  1.

This is the desired result:

LUCE  LUCENum  RESULT

ON         1             1

LIGHT    2             1

LIGHT    2             1

OFF        3             1

ON         1             2

LIGHT    2             2

LIGHT    2             2

LIGHT    2             2

LIGHT    2             2

OFF        3             2

ON         1             3

LIGHT    2             3

OFF        3             3

Attached you will find an example of what I tried to do, but it doesn’t work.

Dear iiiaaa

I don't see how we can solve this problem using loops. However, you can use a Java Snippet.

Here is the code I used to solve your problem:

```// Your custom variables:
int output = -1;

if(c_LuceNum==1){
output++;
}
out_out = output;
```

Thank you very much, Iris. It works!

Always wanting to find a solution which doesn't involve a snippet, here is how you would do it,

row filter node, and filter on ON.

then maths node, using ROWINDEX+1 for the expression, append as new column called RESULT

joiner node, join the result table back to the original column, using RowID, so you have counting up number next to the ON's with the RESULT column.

Times series missing node, on REsULT column, using Last option as missing value handling strategy. This fills in the gaps between the numbers.

hope that helps.

simon.

Thank you very much, Simon. It works also in this way!

Huh. I could have used that one a while ago... Didn't know about the Times Series Missing Value back then. I don't like that you have to resort by RowID though. GroupBy has the option to retain row ordering, but I couldn't get my Joiner to do that.

Here's another tip: The Math Node is kind of a cheat, a Java Snippet in disguise. But there's the Counter Generation Node for the rescue.