我正在编写一个辅助函数,如果列组包含特定的列名,则重命名该列组。
Names Default To Here( 1 );
dt = Open( "$SAMPLE_DATA/Cities.jmp" );
/*//Use if already existing
dt = Data Table ("Cities");
*/
dt << group columns( "xy", {:X, :y} );
dt << group columns( "pollutants", :Ozone :: :Lead );
match_col = "X";
new_name = "coordinates";
groups_list = dt << get column groups names;
show(groups_list);
For Each({grp, ig}, groups_list,
cols = dt << get column group( grp );
show(cols);
For Each ({column, ic}, cols,
show(column);
colname = column << Get Name;
If(colname == match_col,
try(dt << rename column group( grp, new_name );)
)
);
);
对于基本的分组列,这种方法效果很好,但如果存在从链接引用继承的分组,则会出错:
Names Default To Here( 1 );
dt = Open( "$SAMPLE_DATA/Cities.jmp" );
dt_state = Open( "$SAMPLE_DATA/State Abbreviations.jmp" );
//Need more than 1 column
dt_state << New Column( "Dummy", Numeric, "Continuous", Format( "Best", 12 ));
//Link Tables
dt_state:Abbreviations << Set Property( "Link ID", 1 );
dt:State << Set Property("Link Reference", Reference Table(dt_state));
/*//Use if already existing
dt = Data Table ("Cities");
dt_state = Data Table( "State Abbreviations" );
*/
dt << group columns( "xy", {:X, :y} );
dt << group columns( "pollutants", :Ozone :: :Lead );
match_col = "X";
new_name = "coordinates";
groups_list = dt << get column groups names;
show(groups_list);
For Each({grp, ig}, groups_list,
cols = dt << get column group( grp );
show(cols);
For Each ({column, ic}, cols,
show(column);
colname = column << Get Name;
If(colname == match_col,
try(dt << rename column group( grp, new_name );)
)
);
);
Firstly, the << Get Name function errors on :Ozone, which worked fine in the simpler example.
即使该错误已解决,日志中关联的列看起来也不同:
column = :Y;
column = Referenced Column(<STRONG>“美国各州[州名]”</STRONG>, Reference(Column(:State), Reference(Column(:U.S. States))));
如何提取文本名称?例如“美国各州[州名]”
您遇到的问题很可能是因为 JMP 引用了错误的数据表。我的建议是始终使用显式列引用 -> 将列(引用)始终转换为字符串,然后根据需要使用不同的格式。在您的情况下,可以像这样:
Names Default To Here(1);
dt = Open("$SAMPLE_DATA/Cities.jmp");
dt_state = Open("$SAMPLE_DATA/State Abbreviations.jmp");
dt_state << New Column("Dummy", Numeric, "Continuous", Format("Best", 12));
dt_state:Abbreviations << Set Property("Link ID", 1);
dt:State << Set Property("Link Reference", Reference Table(dt_state));
dt << group columns("xy", {:X, :y});
dt << group columns("pollutants", :Ozone :: :Lead);
match_col = "X";
new_name = "coordinates";
groups_list = dt << get column groups names;
For Each({grp, ig}, groups_list,
cols = Transform Each({colweirdness}, dt << get column group(grp), AsColumn(dt, colweirdness) << get name);
For Each({colname, ic}, cols,
If(colname == match_col,
try(dt << rename column group(grp, new_name));
)
);
);