早上好。
使用 JSL 代码,从具有 GOOD / BAD 行的列开始,我想创建一个新的 WEIGHT COLUMN,其中 BAD = 1 且 GOOD = BAD/GOOD 比率。
有JSL可以快速做吗?
提前致谢
詹保罗
这是两个类似的解决方案。
这会在打开的 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
);
)
);
此版本使用列公式来创建“权重”列的值
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
)
);