Keep Columns Matching a List, Remove the RestHi -
I would like to remove columns (based on column names) from a data table that do not match a list. I have following JSL code so far but it is not working.
Input appreciated.
Names Default To Here( 1 );
bigClass = Open( "$SAMPLE_DATA/Big Class.JMP" );
dt = Current Data Table();
col_of_interest = {"name", "age", "weight"};
col_names = dt << Get Column Names( String );
For(j = 1, j <= N Items( col_of_interest ), j++,
For( i = N Items( col_names ), i > 0, i--,
if ( not( col_names[i] == col_of_interest[j],
Remove From( col_names, i ))
);
);
);
dtNew = dt << subset( all rows, columns( col_names ) ); Wait( 0 );
ScriptingWindowsRe: Keep Columns Matching a List, Remove the RestI would loop over all columns in the data table and remove when needed (if you want to keep to your idea, I can also help debugging that):
Names Default To Here(1);
bigClass = Open("$SAMPLE_DATA/Big Class.JMP");
dt = Current Data Table();
col_of_interest = {"name", "age", "weight"};
col_names = dt << Get Column Names(String);
For(i = 1, i <= N Items(col_names), i++,
If(!Contains(col_of_interest, col_names[i]),
dt << Delete Columns(col_names[i]);
);
);
Wait(0);Also I would suggest not using J as variable name, as JMP has a matrix function J().
Edit:
Also there is no need to use Current Data Table() (and you shouldn't) because you already have the reference after opening the table. Also here is my preferred method to remove columns (using associative array to get only the extra columns and removing those):
Names Default To Here(1);
dt = Open("$SAMPLE_DATA/Big Class.JMP");
col_of_interest = {"name", "age", "weight"};
//get columns which are "extra"
col_names_aa = Associative Array(dt << Get Column Names(String));
col_names_aa << Remove(Associative Array(col_of_interest));
col_names_to_remove = col_names_aa << get keys;
Show(col_names_to_remove);
dt << Delete Columns(col_names_to_remove);Re: Keep Columns Matching a List, Remove the RestWhen comparing two lists, I find it easier to make a third list to contain the results while working through the loops and then use the third list to take action when all the looping is complete. The code below may accomplish what you are attempting.
Names Default To Here( 1 );
dt = Open( "$SAMPLE_DATA/Big Class.JMP" );
col_of_interest = {"name", "age", "weight"};
col_names = dt << Get Column Names( String );
col_list = {};
For( i = N Items( col_names ), i > 0, i--,
if(contains(col_of_interest, col_names[i]),
insert into(col_list, col_names[i])
);
);
dtNew = dt << subset( all rows, columns( col_list ) );
Wait( 0 );
Re: Keep Columns Matching a List, Remove the RestThanks for the input and the tip on matrix function J, jthi. Let me retry.