如何在列公式中插入对动态生成的表变量名称的引用,以便稍后可以更改表变量并仍然更新列值。 然后对未知数量的列执行此操作。
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 )
);
最好的,
中号