Subscribe Bookmark RSS Feed

Assigning Column Name (x) output to a matrix?

sornasst

Community Trekker

Joined:

Feb 2, 2016

As part of a longer script, I would like to extract the column names from one table and list those in a new table in a new column.

I have naively tried the following:

dt = Current Data Table ();

m = [];

For(i=8,i<= N Col (dt),i++,

m = m || Column Name ( i );

);

New Table ("Table", New Column ("Headers", character,set values(m)));

I get a error message "Concatenate for Strings or Matrices Only" which make sense since I'm trying to stick a Name into a Matrix.

What is the proper syntax and/or function to make this work?

Thank you.


1 ACCEPTED SOLUTION

Accepted Solutions
Solution

Use a List instead a Matrix​, and then use the Insert Into() function.

dt = Current Data Table ();

m = {};

For(i=8,i<= N Col (dt),i++,

insert into(m,  Column Name ( i ));

);

New Table ("Table", New Column ("Headers", character,set values(m)));

The For() loop may be inefficient for large data tables. In that case a modification of txnelson​'s solution may be better. This version indexes the list returned by Get Column Names to use the items from 8 to the end.

Names Default to Here(1);

dt = Current Data Table();

dt2 = New Table( "Table",

New Column( "Headers",

character,

values( (dt << get column names)[8 :: N Items( dt << get column names )] )

)

);

-Jeff
3 REPLIES
txnelson

Super User

Joined:

Jun 22, 2012

I believe this makes it a little simpler

Names Default to Here(1);

dt = Current Data Table();

dt2 = New Table("Table",New Column("Headers", character, values(dt<<get column names)));

Jim
sornasst

Community Trekker

Joined:

Feb 2, 2016

Hi Jim,

Thank you for your elegant suggestion.

While your script captures all the column names from one file, I would like to be able to skip the first 8 columns to only capture the names of the columns on which I'm computing some stats.

Thank you for your help.

Sincerely,

Thierry

Solution

Use a List instead a Matrix​, and then use the Insert Into() function.

dt = Current Data Table ();

m = {};

For(i=8,i<= N Col (dt),i++,

insert into(m,  Column Name ( i ));

);

New Table ("Table", New Column ("Headers", character,set values(m)));

The For() loop may be inefficient for large data tables. In that case a modification of txnelson​'s solution may be better. This version indexes the list returned by Get Column Names to use the items from 8 to the end.

Names Default to Here(1);

dt = Current Data Table();

dt2 = New Table( "Table",

New Column( "Headers",

character,

values( (dt << get column names)[8 :: N Items( dt << get column names )] )

)

);

-Jeff