在 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 )
);謝謝!
進階統計建模自動化和腳本編寫內容組織資料存取資料混合與清理數據探索和視覺化實驗設計大規模客製化回覆:在 IF 表達式中使用列變數這段程式碼的一個小缺點是:公式中的變數以後將不再定義。
這可以透過公式編輯器中的紅色擺動看出:

結果:當表中的值將來發生變更時,該列將不會自動更新。
使用我從中學到的方法可以產生更健壯的程式碼@jthi :
放NameExpr(作為 Column())裡面表達式() 。
那麼公式將如下所示:

如果輸入值更新,列值將來也會更新:)
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;
回覆:在 IF 表達式中使用列變數再次感謝拜倫,
在嘗試了兩者並閱讀了提供的一些連結之後,最終將兩者結合起來: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 index回覆:在 IF 表達式中使用列變數也許也看看這兩個論點:
柱子()
作為名稱()
您的腳本可能嘗試將 ColSelect 解析為文字字串而不是列名
回覆:在 IF 表達式中使用列變數非常感謝賈莫,
我在你的回覆中有部分解決方案。
儘管如此,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
回覆:在 IF 表達式中使用列變數嗨拜倫,
Show(colSelect) 傳回一個簡單的字串,而不是一個列表(我在傳遞 Dialog 參數時處理了這一點):
colSelect = "從簽到到退出的總時間";
我會研究這個PDF,謝謝回覆:在 IF 表達式中使用列變數由於 ColSelect 來自對話框,因此它實際上可能是項目的清單。
如果計算 ColSelect,會回傳什麼?如果是{:colname},那麼它就是一個項目的列表
嘗試評估 ColSelect[1],如果它來自列表,則會傳回 :colname 。
在這種情況下,eval() 傳回清單。
也看看 Eval list()
有一個 pdf 檔案對於理解本文所附的表達式非常有幫助。
表達式處理函數:第一部分 - 解析 Expr()、NameExpr()、Eval() 等 難題
回覆:在 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