Exclude selected columnsHello,
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 <
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.
Re: Exclude selected columnsThis 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
Re: Exclude selected columnsThank 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;Re: Exclude selected columnsYou 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);Re: Exclude selected columnsSorry 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<BR />col_names = (":Col A", ":Col B", etc);
<BR />//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</CODE></P></LINGO-BODY><LINGO-SUB id="lingo-sub-73693" slang="en-US" mode="NONE">Re: Exclude selected columns</LINGO-SUB><LINGO-BODY id="lingo-body-73693" slang="en-US" mode="NONE"><P>Thanks!!! I also figured that out but it was a bit longer. This is perfect :)</img></P></LINGO-BODY><LINGO-SUB id="lingo-sub-73657" slang="en-US" mode="NONE">Re: Exclude selected columns</LINGO-SUB><LINGO-BODY id="lingo-body-73657" slang="en-US" mode="NONE"><P>Here is the syntax of how to do this:</P>
<PRE><CODE class=" language-jsl">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 )
);