Hi
I have an input data like below
Name | ID | Score1 | Score2 | Total |
J | 245 | 12 | 23 | 35 |
i am passing this into an R code in alteryx workflow and i use randomly generated weights to calculate weighted total. Now i would like to run the R code 10 times, each time generating new random weights and calculate 10 weighted total, then calculate the lowest weight and output it as final result.
my desired output
Name | ID | Score1 | Score2 | Total | weighted total |
J | 245 | 12 | 23 | 35 | 14.3 |
J | 245 | 12 | 23 | 35 | 9.9 |
J | 245 | 12 | 23 | 35 | 16.1 |
J | 245 | 12 | 23 | 35 | 10.5 |
J | 245 | 12 | 23 | 35 | 12.2 |
J | 245 | 12 | 23 | 35 | 8 |
J | 245 | 12 | 23 | 35 | 11.3 |
J | 245 | 12 | 23 | 35 | 15.5 |
J | 245 | 12 | 23 | 35 | 9.1 |
J | 245 | 12 | 23 | 35 | 13.6 |
After generating the above output table, my final result will be the one with lowest weighted total (8)
I have attached the workflow herewith.
Any suggestion on how to do this in a looped manner?
Solved! Go to Solution.
Hi @GaRaGe
Yes you could turn it into a batch macro or iterative macro.
I'm not being able to see your R tool, since I don't have it installed, but it's pretty easy. Here's a mockup of how your macro would look:
This is an iterative macro example.
Select tool before the Loop Output is so you take the field of Weight that you created, because the loop output will feed the input of the next iteration (it must have the same columns).
Numeric Drop Down is to give you the choice of how many times you want to run the macro.
Then you use your macro, a Sort tool to put the minimum value of weight on top, and sample tool to select first record.
Cheers.
Hi @GaRaGe,
Since you're doing R anyway, you could include the logic in your R code:
input<-read.Alteryx("#1",mode="data.frame") set.seed(1234) min_weight <- input$Score1 + input$Score2 for (i in 1:10) { weights <- runif(2,min=0.1,max=0.9)
weighted_tot <- (input$Score1*weights[1]) + (input$Score2*weights[2]) if (weighted_tot < min_weight) { min_weight <- weighted_tot } } input$weighted_tot <- min_weight write.Alteryx(input,1) write.Alteryx(as.data.frame(weights),2)
I'm sure that can be cleaned up a bit, but that's the basic idea.
Hope that helps!
John