consecutive count

consecutive count
Hello I want to know if this is something possible to do


I need to make a flow in knime that tells me at the end how many consecutive there were, for example if, yes, they are not 2 consecutive, while if, no, yes, they are not 1 consecutive, yes, yes, yes, yes, yes, no , they are not 5 consecutive is this possible?

Hola @Jalvear

Challenging problem. Please find a possible solution to your question:

20220210 Pikairos consecutive count.knwf (40.0 KB)

The final text generation is based on the following regex function in the -String Manipulation node:

strip( regexReplace( regexReplace( regexReplace( string( $Pivot$), "^(0)$", " Nunca"), "^(1)$", " $1 mes sin TX"), "(^[0-9].*)", " $1 meses sin TX")) 

Basically the string (regexReplace() and strip()) functions are doing the following:

  1. Replace a number 0 by " Nunca"
  2. Else replace number 1 by " 1 mes sin TX"
  3. Otherwise replace number starting by 0-9 digits by the number followed by " meses sin TX".
  4. Strips any white space characters from the beginning and end of resulting final string.

This version should be robust to “several digits” number going beyond 10 months and make the difference between 0, 1, 10, 101 months, etc.

Hope it helps.

Best

Ael

4 Likes

Thank You

however I am curious if it is possible to do this, I have yes yes yes, no , no, no, yes , yes, this means that only take the last two yes they were not 4 or 6 only 2

you could use a lag node to compare with the prior row and check for changes. This created helper column can then be used to “restart” the count

It would be more complicated but I guess it is possible. The whole thing is to find an algorithm for it that best suits the way KNIME works. As you could see, it was solved in this case using two tricks: one was to replace “No” by missing values and the other one was to count using the sum aggregation of “1” values (eq. to counting the number of Yes).

But the above cannot work in the case you mention which is more complex. I’ll have a thought about though :wink:

Thank you so much for all

1 Like

If the number of input columns is known, a static solution could look like this:

  1. aggregate input columns into one column with concatenation
  2. use n+1 rules to determine largest consecutive yes-sequence; n being the number of input columns

It works, but I’m not particularily proud of myself :laughing:. A dynamic solution probably requires one of the scripting nodes.
count consecutive column values
count consecutive column values.knwf (61.7 KB)

1 Like

Hi @Jalvear

I guess this new version implements what you need. It is a modification of the initial solution:

20220212 Pikairos consecutive count of last Yes sequence.knwf (99.9 KB)

Hope it helps

Best

Ael

2 Likes

Thank you for all,
They helped me a lot with this project

2 Likes

Thanks @Jalvear for validating the solution and happy to help.

Best

Ael

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