Subscribe Bookmark RSS Feed

Select columns contained in user supplied list

terapin

Community Trekker

Joined:

Jun 23, 2011

I'm trying to figure out how to use a user specified list to select those data columns contained in the list.  The user supplied list may contain more or less names than present in data table.  For example, I've tried the following but I can't seem to get it to work.  Any ideas would be appreciated.  Thanks

dt = open("$SAMPLE_DATA/Big Class.jmp");

// Get list of data column names

col_list = dt << Get Column Names ( String );

// User specified column names

cc = { sex, weight, test, new, old };

 

for( i=1, i<=ncol( cc ), i++,

       if(contains( col_list, cc ),

       Column ( dt, col_list ) << Set Selected ( 1 )

       )

);

1 ACCEPTED SOLUTION

Accepted Solutions
ms

Super User

Joined:

Jun 23, 2011

Solution

I was not looking into the details of the output. You are looping over cc, then you should refer to cc rather than col_list to get the desired result.

dt = Open( "$SAMPLE_DATA/Big Class.jmp" );

// Get list of data column names

col_list = dt << Get Column Names( string );

// User specified column names

cc = {sex, weight, test, new, old};

For( i = 1, i <= N Items( cc ), i++,

  If( Contains( col_list, Char( cc[i] ) ),

  Column( dt, Char( cc[i] ) ) << Set Selected( 1 )

  )

);


//This works too

For( i = 1, i <= N Items( cc ), i++,

  If( Contains( col_list, Char( cc[i] ) ),

   :(cc[i]) << Set Selected( 1 )

  )

);

4 REPLIES
ms

Super User

Joined:

Jun 23, 2011

Almost there. Use N Items() instead of ncol() for lists. And to match two items both need  to of the same data type. So, either provide the user supplied list as a list of strings or use Char()as below.

dt = Open( "$SAMPLE_DATA/Big Class.jmp" );

// Get list of data column names

col_list = dt << Get Column Names( string );

// User specified column names

cc = {sex, weight, test, new, old};

For( i = 1, i <= N Items( cc ), i++,

  If( Contains( col_list, Char( cc[i] ) ),

  Column( dt, col_list[i] ) << Set Selected( 1 )

  )

);

terapin

Community Trekker

Joined:

Jun 23, 2011

Hi MS,

Thanks for the tips.  Although the code you provided runs it doesn't select just those columns listed in the user specified list.  For example, the code listed above selects the first two columns ( name and sex) even though sex isn't in the user specified list.  I was thinking this list construct was going through and finding those column numbers in col_list that match those located in cc and then selecting them.  It appears that it is actually selects the columns based on position in cc, not name.  Is it possible to select the columns according to matched names, not position?

ms

Super User

Joined:

Jun 23, 2011

Solution

I was not looking into the details of the output. You are looping over cc, then you should refer to cc rather than col_list to get the desired result.

dt = Open( "$SAMPLE_DATA/Big Class.jmp" );

// Get list of data column names

col_list = dt << Get Column Names( string );

// User specified column names

cc = {sex, weight, test, new, old};

For( i = 1, i <= N Items( cc ), i++,

  If( Contains( col_list, Char( cc[i] ) ),

  Column( dt, Char( cc[i] ) ) << Set Selected( 1 )

  )

);


//This works too

For( i = 1, i <= N Items( cc ), i++,

  If( Contains( col_list, Char( cc[i] ) ),

   :(cc[i]) << Set Selected( 1 )

  )

);

terapin

Community Trekker

Joined:

Jun 23, 2011

MS,

Thanks for your help with this.  I see now that my thinking about which column to loop over was what was causing me some of my earlier grief.  I assumed I would loop over and select the columns listed in the data table variable list, not the user specified list.  Always learning something new here.  Thanks.