cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Check out the JMP® Marketplace featured Capability Explorer add-in
Choose Language Hide Translation Bar
gianpaolo
Level IV

create a weight Column

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  

Gianpaolo Polsinelli
1 ACCEPTED SOLUTION

Accepted Solutions
txnelson
Super User

Re: create a weight Column

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
	)
);
Jim

View solution in original post

4 REPLIES 4
uday_guntupalli
Level VIII

Re: create a weight Column

Can you kindly provide sample data of your column to understand what GOOD / BAD looks like ?
Best
Uday
gianpaolo
Level IV

Re: create a weight Column

Split Weight Split where:
GOOD 0.4 BAD=1
BAD 1 GOOD = #BAD/#GOOD
GOOD 0.4 in this case 4/10 =0.4
GOOD 0.4
GOOD 0.4
GOOD 0.4
GOOD 0.4
GOOD 0.4
GOOD 0.4
GOOD 0.4
GOOD 0.4
BAD 1
BAD 1
BAD 1
Gianpaolo Polsinelli
txnelson
Super User

Re: create a weight Column

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
	)
);
Jim
gianpaolo
Level IV

Re: create a weight Column

Great Jim, Thanks very much.

i will adapt your code to my job  

Gianpaolo Polsinelli