Creating replicate rows in KNIME

Hello

I am after a solution to create replicate rows in KNIME where the number of replicate rows is given by a column value. Suppose I have the following table:

col1  col2  col3  col4

A      B       C      3

...

I would like to replicate each row x times according to col4 (in this case 3 times). So the resulting table would like like this:

col1   col2   col3  col4

A       B        C       1

A       B        C       1

A       B        C       1

...

Any help would be much appreciated

Thanks, K

Hi,

it is quite easy to achieve with two nested loops and the use of flow variables. See the attached workflow.

Cheers,
Marco.

1 Like

Fantastic, thank you Marco for such a quick and useful response!

K

Alternatively a Java snippet with the following code would produce collection columns, which could then be split by the UngroupBy node:

newCol1 = new String[col4];
newCol2 = new String[col4];
newCol3 = new String[col4];
newCol4 = new Integer[col4];
for (int i = 0; i < col4; i++) {
    newCol1[i] = col1;
    newCol2[i] = col2;
    newCol3[i] = col3;
    newCol4[i] = 1;
}

I know Java snippets aren't very KNIMEy, but why use 7 nodes when you can get away with 2? :)

Or use the one to many node from the data generation plugins. It does exactly this.

2 Likes

Hello @marco_ghislanzoni,
The workflow has been realy helpful. I am also trying to achieve the same results.
But in a row the iteration count value is 0. The counting loop stops there and throws an error message
" cannot loop fewer than once"
and doesn’t iterate to the next row.
Can you help me with this
[ example dataset:
image

Thanks

If the count associated with a particular row is zero, then there’s nothing to loop over - so maybe just filter out those records beforehand?

1 Like

Hello @ScottF,
Thanks for the Idea.
And I have a column for month in here.So if the the iteration count in 5, i want to get the following 5 months in the output. If its 24, i want to get next 24 months in the output column.
can you help me with that.
Example snapshot for required output if the count =4
image

If you are using the existing workflow provided above, you can modify it by adding a couple of nodes between the 2 Loop End nodes:

2019-04-17%2015_54_20-KNIME%20Analytics%20Platform

Within the Rule Engine is just a primitive way to assign abbreviated month strings based on the counter:

2019-04-17%2015_55_20-Dialog%20-%203_14%20-%20Rule%20Engine

There’s probably a better way to do this, but it works!

2019-04-17%2015_56_21-Collected%20results%20-%203_6%20-%20Loop%20End%20(Next%20row)

2 Likes

Hello @ScottF,
Thanks for the solution.
I need to produce the following months . If i running the WF today, the next rows have the values
May
June
July

So it the month value should start from the present the and following number of months should be based on the count

Thanks

In that case, you probably just need a flow variable corresponding to the current month, which you can then pass into the Counter Generation node as a starting point.

Such a flow variable could be generated using the Date&Time Input node to get the current date, and then parsing out the month.

EDIT: See attached updated workflow.

032_forum_ScottF_edits.knwf (32.1 KB)

3 Likes

@rsherhod an you give an example for this method please

Hi there @Arnold,

there is node for multiplying row based on value in that same table - One Row to Many. Would that work for you?

Br,
Ivan

2 Likes

Thanks so much for posting this! This was also exactly what I needed!