根据列表创建列子集,其中可能包含不存在的列嗨,用户们,
我通读了与该主题相关的先前讨论,但没有找到所有答案。
我想知道如何基于给定的列列表创建列子集。 我知道当给定列表中的列存在于您要子集化的数据表中时,您可以轻松地做到这一点。
我想知道的是,如何使脚本更健壮,以便在列表中的给定列不存在的情况下,不会出现“在访问或评估“错误参数”时找不到列”的错误。
例如,在下面的脚本中,我仍然希望能够创建带有列,名称,年龄和高度的“ Big Class.jmp”子集,因为它们存在,但是由于我的脚本不给定列表中存在的列称为“ none”。
我尝试将Try()放在代码周围,但没有成功。
cols_of_interest = {:name, :age, :height, :none};
show(cols_of_interest);
dt = Open( "$SAMPLE_DATA/Big Class.jmp" );
big_class_cols = dt << Get Column Names();
show(big_class_cols);
subDt1 = dt << Subset(
Columns( cols_of_interest ),
Output Table Name( "Big Class 2" )
);
”
谢谢,
西蒙
数据表脚本编写回复:根据可能包含不存在的列的列表创建列子集这是处理此问题的简单方法。它只是检查数据表中是否存在cols_of_interest列表中的每一列,如果不存在,则将其从cols_of_interest列表中删除。
names default to here(1);
cols_of_interest = {:name, :age, :height, :none};
show(cols_of_interest);
dt = Open( "$SAMPLE_DATA/Big Class.jmp" );
For(i=nitems(cols_of_interest), i>=1, i--,
if(try(column(dt,char(cols_of_interest[i]))<<GET name=""></GET>回复:根据可能包含不存在的列的列表创建列子集这是一种使用 <
dt = Open( "$SAMPLE_DATA/Big Class.jmp" );
cols_of_interest = {name, age, height, none}; //names without scoping colon
AA1 = Associative Array( dt << Get Column Names() );
AA2 = Associative Array( cols_of_interest );
cols = AA2 << intersect( AA1 ) << get keys;
subDt1 = dt << Subset( Columns( cols ), Output Table Name( "Big Class 2" ) );