Good morning.
using JSL code, starting from a column with GOOD / BAD rows, i would like to create a new WEIGHT COLUMN, where BAD =1 and GOOD = Ratio BAD/GOOD.
There is JSL to do it quickly?
thanks in advance
Gianpaolo
Here are two similar solutions.
This one generates the new column in open JSL
Names Default To Here( 1 ); // Create the sample data table dt = New Table( "Good Bad Ratio", Add Rows( 14 ), New Column( "Good_Bad", Character, "Nominal", Set Values( {"GOOD", "BAD", "GOOD", "GOOD", "GOOD", "GOOD", "GOOD", "GOOD", "GOOD", "GOOD", "GOOD", "BAD", "BAD", "BAD"} ) ) ); // Create the new column using a formula for the calculation dt << New Column( "Weight", formula( If( Row() == 1, TheGoodCount = Col Number( If( :Good_Bad == "GOOD", 1, . ) ); TheBadCount = Col Number( If( :Good_Bad == "BAD", 1, . ) ); Ratio = TheBadCount / TheGoodCount; ); If( :Good_Bad == "GOOD", Ratio, :Good_Bad == "BAD", 1 ); ) );
This version uses a column formula to create the values for the column Weight
Names Default To Here( 1 ); // Create the sample data table dt = New Table( "Good Bad Ratio", Add Rows( 14 ), New Column( "Good_Bad", Character, "Nominal", Set Values( {"GOOD", "BAD", "GOOD", "GOOD", "GOOD", "GOOD", "GOOD", "GOOD", "GOOD", "GOOD", "GOOD", "BAD", "BAD", "BAD"} ) ) ); // Calculate the Ratio TheGoodCount = Col Number( If( dt:Good_Bad == "GOOD", 1, . ) ); TheBadCount = Col Number( If( dt:Good_Bad == "BAD", 1, . ) ); Ratio = TheBadCount / TheGoodCount; // Add the new column and populate it dt << New Column( "Weight" ); For Each Row( If( dt:Good_Bad == "GOOD", dt:Weight = Ratio, dt:Good_Bad == "BAD", dt:Weight = 1 ) );
Great Jim, Thanks very much.
i will adapt your code to my job