Random Replacement

I have two columns. I want to replace data in Column with randomly selected data from Column. However I need to ensure that same string in Column 1 is replaced with the same string in Column 2. For e.g.

Table 1:

  • Backup Software
  • Word Processor
  • Backup Software
  • Anti-virus
  • Anti-virus
  • Backup Software

Table 2:

  • Cat
  • Dog
  • Fish

The result should be:

  • Fish
  • Dog
  • Fish
  • Cat
  • Cat
  • Fish

Notice that Backup Software is replaced with randomly select item “Fish”. Anti-virus is replaced by Cat. And Wordprocessor is replaced by Dog

I am using a Dictionary based Cell-replacement strategy for this. But it is rather cumbersome when dealing with large amount of data and columns.

Any easy way of doing this in knime?

Thanks! :slight_smile:

Hello @saqib,

in your example you happen to have the same number of unique values in both tables, is this a general assumption you can make? If so, then you could use the following strategy:

  1. Apply Group By to Table 1 to get the unique values.
  2. Use the Column Appender to create an initial dictionary.
  3. Use Target Shuffling to shuffle one of the columns which provides you with a randomized dictionary.
  4. Use the Cell Replacer to replace the cells in Table 1 using the dictionary generated in the previous step.

Cheers,
Adrian

2 Likes

Hi @saqib

There is actually a node for this, the Random Matcher does very close what you need.

Made you an example workflow here:

2 Likes

Hello @nemad and @Iris. Thanks for the feedback. I am currently using the strategy that you suggested (using dictionary based cell-replacer). But it becomes very complex when the number of columns that need to be replaced increases. I was looking for a simpler solution.

Any thoughts?

Thanks,
Saqib

For multiple columns, you can use the Column List Loop Start and Loop End (Column Append) to apply the same technique to all of your columns.
Admittedly, this is slightly more complicated but once you got it set up, you can deal with arbitrary many columns.

2 Likes

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