如何在列公式中插入對動態生成的表變量名稱的引用,以便稍後可以更改表變量並仍然更新列值。 然後對未知數量的列執行此操作。
dt = Current Data Table();
NewCol = function({ColNr},
//Set a name table variable with the name of the column and RMSE appended to it
dt << Set Table Variable( concat(Column(ColNr)<< Get Name(),"RMSE"), 0 );
//Create a new column with the same name as the original column but with " with RMSE" at the end.
//Place a formula in this column which takes the original column value and add random RMSE as specified by the table variable to it.
Eval(
substitute(
expr(
dt<<NEW column=""></NEW>
感謝您挑戰我,我再試一次並解決了它:)
你是對的,創建列名效果很好。 它不適用於創建新列表達式的公式部分。
我要做的就是在解析函數中將 :Name( "...") 放在列和表變量名稱周圍。
dt = New Table( "Untitled 9",
Add Rows( 0 ),
New Column( "Pred Formula Thickness (A)",
Numeric,
"Continuous",
Format( "Best", 12 ),
Set Values(
[-1.09815926721176, -0.143977108758327, -1.60078929360768,
-0.340409665497429, -0.581215173360104, 0.233876291958488,
-0.357108531367114, 0.0704280592629584, 1.86236889119533,
0.669812241478143, -0.00962200322380528, 1.33041117732878,
0.533367791752501, 2.24183743599671, -0.506060621538248]
),
Set Display Width( 145 )
)
);
ColNr = 1;
//Create the table variable based on the column name with RMSE added to it.
dt << Set Table Variable( concat(column(ColNr)<< Get Name()," RMSE"), 0 );
//Create the text for the column formula. Notice that the formula stored in test is correct. It is the 'column name' + 'the table variable' multiplied with random normal.
test = ":Name( \!"" || char(column(ColNr)<< Get Name()) || "\!") + :Name( \!"" || char(column(ColNr)<< Get Name()) || " RMSE\!") * Random Normal()";
//But when putting this in a column formula equation, the parser stops parsing after the second repeat of the column name. In an ideal world I would just refer to the table variable directly.
eval(parse( "dt<<NEW column=""></NEW>
Eval(Parse()) 方法可以輕鬆處理複雜的列名。 您只需要認識到此類問題可能存在即可。 這是一個簡單的例子
Names Default to here(1);
dt = New Table( "big class",
Add Rows( 40 ),
New Column(
"Complex column a+7/(1_2)",
Numeric,
"Continuous",
Format( "Fixed Dec", 5, 0 ),
Set Values(
[59, 61, 55, 66, 52, 60, 61, 51, 60, 61, 56, 65, 63, 58, 59, 61, 62, 65, 63,
62, 63, 64, 65, 64, 68, 64, 69, 62, 64, 67, 65, 66, 62, 66, 65, 60, 68, 62,
68, 70]
)
),
New Column(
"weight",
Numeric,
"Continuous",
Format( "Fixed Dec", 5, 0 ),
Set Values(
[95, 123, 74, 145, 64, 84, 128, 79, 112, 107, 67, 98, 105, 95, 79, 81, 91,
142, 84, 85, 93, 99, 119, 92, 112, 99, 113, 92, 112, 128, 111, 105, 104,
106, 112, 115, 128, 116, 134, 172]
)
)
);
col names = dt<<GET column="" names=""></GET>
我仍在尋找更強大的解決方案,因為 parse(eval()) 方法不能很好地處理表變量名稱或列名稱中的 () 。
我懂了。 由於您是動態生成列名稱,因此您可以使用多種策略。
第一個是在運行時生成的列名稱列表上僅使用幾個“substitute into()”命令。 在大多數情況下,這通常效果很好。
其次,您可以使用 Eval(Parse()) 方法,在該方法中使用連接命令以編程方式構建 JSL 字符串。 這是我的銀彈。 特別是與列屬性和操作相關的內容。 它消除了對所有額外 Eval() 命令的需要,因為腳本基本上是將完成的命令“鍵入”為字符串並將其轉換為表達式。 然後您只需運行該表達式即可。回复:JSL:如何在列公式內創建對錶變量的引用問題是我無法對列公式中的表變量名稱進行硬編碼,因為列數以及這些列的名稱未知。 創建表變量是可行的。 在列公式中引用它們是困難的部分。回复:JSL:如何在列公式內創建對錶變量的引用您首先生成表變量:
dt << New Table Variable("RMSE_1", 30);
在生成新的公式列之前,您可能會在循環中執行此操作。
之後,表變量的引用與列類似,因此從上面:
// Add the table variable
dt << New Table Variable( "RMSE_1", 30 );
// Add the column referencing the table variable
dt << New Column( "Column 1",
Numeric,
"Continuous",
Format( "Best", 12 ),
Formula( :RMSE_1 ^ 2 )
);
最好的,
中號