在 IF 表达式中使用列变量亲爱的社区,
下面,我使用了对话框带来的几个变量:
- colSelect:要分析的列
- quantVal1 & quantVal2:分位数值
在下面的脚本中,我创建了一个“类别”列,然后将其制成表格以获得每个分位数的数量。
两个问题:
- 用 Eval() 方法替换硬编码的列名称不起作用
- 我如何测试该列是否已经存在,如果是,更新它的公式?
这将允许使用新的列和分位数刷新我的整个分析过程,而不必从头开始加载和处理数据表。
//Custom Quantiles amounts
dt << New Column( "Categories",
Numeric, "Continuous",
Format( "Best", 9 ),
Formula(
If( Eval( colSelect )/*:Total Time From CheckIn Collector To Exit*/ <= quantVal1,
1,
If( Eval( colSelect )/*:Total Time From CheckIn Collector To Exit*/ <= quantVal2,
2,
3
)
)
),
Set Property( "Value Order", {} ),
Set Property(
"Color Gradient",
{{"Green to Orange to Red", 4099, {{0, 227, 0}, {255, 255, 0}, {252, 11, 11}}, {0, 0.5, 1}}, Range( {1, 3, 1} )}
),
Color Cell by Value,
Set Display Width( 74 )
);谢谢!
脚本Re: 在 IF 表达式中使用列变量此代码的一个小缺点:公式中的变量以后将不再定义。
这可以通过公式编辑器中的红色摆动看出:

结果:当表中的值将来发生变化时,该列将不会自动更新。
可以使用我从中学到的方法生成更健壮的代码@jthi :
放NameExpr(作为列())里面表达式() .
然后公式将如下所示:

如果输入值得到更新,列值将在未来更新 
Names Default To Here(1);
dt = Open("$SAMPLE_DATA/Big Class.jmp");
colSelect ="age";
quantVal1 = 13;
quantVal2 = 16;
//Custom Quantiles amounts
Eval(Eval Expr(dt << New Column( "Categories",
Numeric, "Continuous",
Format( "Best", 9 ),
Formula(
If( Expr( NameExpr(As Column(dt, colSelect)) )/*:Total Time From CheckIn Collector To Exit*/ <= Expr(quantVal1),
1,
If( Expr( Name Expr(As Column(dt, colSelect)) )/*:Total Time From CheckIn Collector To Exit*/ <= Expr(quantVal2),
2,
3
)
)
),
Set Property( "Value Order", {} ),
Set Property(
"Color Gradient",
{{"Green to Orange to Red", 4099, {{0, 227, 0}, {255, 255, 0}, {252, 11, 11}}, {0, 0.5, 1}}, Range( {1, 3, 1} )}
),
Color Cell by Value,
Set Display Width( 74 )
)));
dt:age[12]=40;
Re: 在 IF 表达式中使用列变量再次感谢拜伦,
在尝试了两者并阅读了提供的一些链接之后,它最终是两者的混合 whihc 有效:AsColumn()
谢谢大家!
Names Default To Here(1);
dt = Open("$SAMPLE_DATA/Big Class.jmp");
colSelect = "age";
quantVal1 = 13;
quantVal2 = 16;
//Custom Quantiles amounts
Eval(Eval Expr(dt << New Column( "Categories",
Numeric, "Continuous",
Format( "Best", 9 ),
Formula(
If( AsColumn( colSelect )/*:age*/ <= quantVal1,
1,
If( AsColumn( colSelect )/*:age*/ <= quantVal2,
2,
3
)
)
),
Set Property( "Value Order", {} ),
Set Property(
"Color Gradient",
{{"Green to Orange to Red", 4099, {{0, 227, 0}, {255, 255, 0}, {252, 11, 11}}, {0, 0.5, 1}}, Range( {1, 3, 1} )}
),
Color Cell by Value,
Set Display Width( 74 )
)));
colList = dt << Get Column Names("String");
Show(Contains(colList, "Categories")); //will return indexRe: 在 IF 表达式中使用列变量也许也看看这两个论点:
柱子()
作为名字()
您的脚本可能试图将 ColSelect 解析为文本字符串而不是列名
Re: 在 IF 表达式中使用列变量非常感谢 Jarmo,
我在你的回复中有部分解决方案。
尽管如此, colSelect 技巧仅适用于给定的数值,不适用于列名(忘记提及)
感谢您的示例,这里有更多的外观(Expr(colSelect)在此更新的上下文中不起作用):
Names Default To Here(1);
dt = Open("$SAMPLE_DATA/Big Class.jmp");
colSelect = "age";
quantVal1 = 13;
quantVal2 = 16;
//Custom Quantiles amounts
Eval(Eval Expr(dt << New Column( "Categories",
Numeric, "Continuous",
Format( "Best", 9 ),
Formula(
If( /*Expr( colSelect )*/:age <= quantVal1,
1,
If( /*Expr( colSelect )*/:age <= quantVal2,
2,
3
)
)
),
Set Property( "Value Order", {} ),
Set Property(
"Color Gradient",
{{"Green to Orange to Red", 4099, {{0, 227, 0}, {255, 255, 0}, {252, 11, 11}}, {0, 0.5, 1}}, Range( {1, 3, 1} )}
),
Color Cell by Value,
Set Display Width( 74 )
)));
colList = dt << Get Column Names("String");
Show(Contains(colList, "Categories")); //will return index
Re: 在 IF 表达式中使用列变量嗨拜伦,
Show(colSelect) 返回一个简单的字符串,而不是一个列表(我在传递 Dialog 参数时处理了这一点):
colSelect = "从签到到退出的总时间";
我将探索此 PDF,谢谢Re: 在 IF 表达式中使用列变量由于 ColSelect 来自对话框,它实际上可能是一个项目列表。
如果评估 ColSelect,返回什么?如果它是 {:colname},那么它是一个包含一项的列表
尝试评估 ColSelect[1],如果它来自列表,则将返回 :colname。
在这种情况下,eval() 返回列表。
也看看 Eval list()
有一个 pdf 非常有助于理解这篇文章所附的表达式。
表达式处理函数:第一部分 - 揭开 Expr()、NameExpr()、Eval()、... 难题
Re: 在 IF 表达式中使用列变量1.好线程如何替换公式中的值:
使用 Eval Insert、Eval Expr、Parse 和 Substitute 将一个表达式插入另一个表达式
如果您不需要公式,我建议您使用 << 设置每个值。
2.您可以使用 dt << Get Column Names("String") 获取列名列表并使用 Contains() 检查该列表中的列
这应该工作:
Names Default To Here(1);
dt = Open("$SAMPLE_DATA/Big Class.jmp");
colSelect = 1;
quantVal1 = 1;
quantVal2 = 2;
//Custom Quantiles amounts
Eval(Eval Expr(dt << New Column( "Categories",
Numeric, "Continuous",
Format( "Best", 9 ),
Formula(
If( Expr( colSelect )/*:Total Time From CheckIn Collector To Exit*/ <= Expr(quantVal1),
1,
If( Expr( colSelect )/*:Total Time From CheckIn Collector To Exit*/ <= Expr(quantVal2),
2,
3
)
)
),
Set Property( "Value Order", {} ),
Set Property(
"Color Gradient",
{{"Green to Orange to Red", 4099, {{0, 227, 0}, {255, 255, 0}, {252, 11, 11}}, {0, 0.5, 1}}, Range( {1, 3, 1} )}
),
Color Cell by Value,
Set Display Width( 74 )
)));
colList = dt << Get Column Names("String");
Show(Contains(colList, "Categories")); //will return index