你好,
我正在学习使用表达式操作,但我很难理解它。
在下面代码的第一部分中,我可以成功地将字符串列名插入到 column() 表达式中,但是我无法将生成的表达式插入到列表中。
第二部分拆分交互项(这是来自 SLS 的效果列表),将它们插入到它们自己的 column() 表达式中,将它们重新组合成交互项,然后将其插入到列表中。此部分不起作用。
有没有人有任何有用的提示或看到我对表达式操作如何工作的误解?提前谢谢你!
names default to here(1);
// Basic need below - but only works for single column names
collist = { "Col 1", "Col 2", "Col 3", "Col 4", "Col 5" };
newlist = {};
for( i = 1, i <= N items(collist), i++,
// Empty arg to identify cols by names
name_arg = Expr( column() );
insert into( name_arg, eval insert( collist[i] ) ); // result: column("Col 1"), or other cols inserted <-- Working as intended
show( name_arg ); // Shows successful insert into's
insert into( newlist, insert into( name_arg, eval insert( collist[i] ) ) );
// Nested insert into doesn't work
// Need to insert the result of the first insert into, into newlist
);
show( newlist ); // Currently shows empty list
///////////////////////////////////////// DOES NOT WORK, BELOW
// Advanced flow - interaction detection
collist2 = { "Col 1", "Col 2", "Col 3", "Col 4", "Col 5", "Col 1*Col 2", "Col 2*Col 4", "Col 5*Col 5" };
newlist2 = {};
for( a = 1, a <= N items( collist2 ), a++,
// Empty arg to identify cols by names
name_arg = Expr( column() );
// For whatever the match below, the result must be added to newlist2
// Regex works, as per regex101.com
if( !contains( collist2[a], "*" ), insert into( name_arg2, eval insert( collist2[a] ) ), // single term insert, refer to basic needs flow above
insert into( name_arg2, eval insert( regex( collist2[a], "^(.*)\*(.*)$", "\1" ) ) ); // Breaks apart 1st group to insert into column()
insert into( name_arg2, eval insert( regex( collist2[a], "^(.*)\*(.*)$", "\2" ) ) ); // Breaks apart 2nd group to insert into column()
// The line below is a mockup of joining back together the first and second group together as interaction terms - doesn't work
//eval expr( expr( insert into( name_arg2, eval insert( regex( mylist2[a], "^(.*)\*(.*)$", "\1" ) ) )*insert into( name_arg2, eval insert( regex( mylist2[a], "^(.*)\*(.*)$", "\2" ) ) );
)
);
Desired_output = { column("Col 1"), column("Col 2"), column("Col 3"), column("Col 4"), column("Col 5"), column("Col 1")*column("Col 2"),
column("Col 2")*column("Col 4"), column("Col 5")*column("Col 5") };