在 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