cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Try the Materials Informatics Toolkit, which is designed to easily handle SMILES data. This and other helpful add-ins are available in the JMP® Marketplace
Choose Language Hide Translation Bar
aumair
Level III

Exclude selected columns

Hello, 

I get a varying list of columns from a source and select these columns in my data table. The data table has many other columns, but I want to hide/exclude the columns which are not in the list provided to me while my script is running and doing some analysis...

 

I can select all the columns which I don't need by line below:

dt < <invert column selection (Provided_List) 

Next step: Exclude these selected columns. How do I do that?

 

From scripting book, I can see column("X") << exclude(1) would exclude the column with name X, but is there a method to exclude already selected columns?

Thanks. 

6 REPLIES 6
txnelson
Super User

Re: Exclude selected columns

Here is the syntax of how to do this:

Names Default To Here( 1 );
dt = Open( "$SAMPLE_DATA/big class.jmp" );

Column( dt, 4 ) << set selected( 1 );
Column( dt, 5 ) << set selected( 1 );

selcols = dt << get selected columns;
For( i = 1, i <= N Items( selcols ), i++,
	Column( dt, selcols[i] ) << exclude( 1 )
);
Jim
aumair
Level III

Re: Exclude selected columns

Thanks!!! I also figured that out but it was a bit longer. This is perfect :)

StarfruitBob
Level VI

Re: Exclude selected columns

Sorry to resurrect this thread, but I have the same question and I wanted direction if this approach like this would work and how to improve it:

//List of wanted columns
col_names = (":Col A", ":Col B", etc);
//Iterates through list and uses the string names to identify and select for( i = 1, i <= N Items(dt), i++, Column( dt, col_names[i] ) << set selected ); dt << Invert Column Selection

Learning every day!
jthi
Super User

Re: Exclude selected columns

You should use N Items(col_names) instead of N Items(dt) in the for loop.

Names Default To Here(1);

dt = Open("$SAMPLE_DATA/Big Class.jmp");
col_names = {"age", "sex", "weight"};
for( i = 1, i <= N Items(col_names), i++,
    Column( dt, col_names[i] ) << set selected;
);
dt << Invert Column Selection;

Also you can make the code simpler by using Select Columns

Names Default To Here(1);

dt = Open("$SAMPLE_DATA/Big Class.jmp");
col_names = {"age", "sex", "weight"};
wait(1); // for demo purposes
dt << Select Columns(col_names);
wait(1); // for demo purposes
dt << Invert Column Selection;

You can also avoid inverting by selecting correct columns directly, but it is most likely more difficult to understand

Names Default To Here(1);

dt = Open("$SAMPLE_DATA/Big Class.jmp");
col_names = Associative Array({"age", "sex", "weight"});
all_col_names = Associative Array(dt << Get Column Names("String"));
all_col_names << Remove(col_names);
dt << Select Columns(all_col_names << get keys);
-Jarmo
StarfruitBob
Level VI

Re: Exclude selected columns

Thank you for your reply, jthi!  That was fast!

Just saw your reply. I've been trying different things and I came up with a very simple 2 line solution.

cols = {"Col A", "Col B"};
dt << Select Columns(cols) << Invert Column Selection;

I've never seen associative arrays used, and when I tried to use one in place of what I currently have for col, Select Columns selected a column that wasn't in cols, but was only 1 of the inverted selection. Strange.  Do you know why that might be?

cols = Associative Array({"Col A", "Col B"});
dt << Select Columns(cols) << Invert Column Selection;
Learning every day!
jthi
Super User

Re: Exclude selected columns

This is because Associative Arrays will save key value pair and most likely when you use Select Columns directly with associative array it doesn't really know what to do. Usually it shouldn't select anything and then inverting it would select all columns (there might be some cases where the associative array would evaluate as 1 so it would select first column). 

 

To get they keys out of associative array, you can use << get keys. 

 

Names Default To Here(1);

cols_list = {"Col A", "Col B"};
cols_aa = Associative Array({"Col A", "Col B"});

Show(cols_list);
Show(cols_aa);
Show(cols_aa << get keys);

You can read more about associative arrays from JMP Help page Scripting Guide > Data Structures > Associative Arrays . I this case I might have used them due to their set operations

 

-Jarmo