Subscribe Bookmark RSS Feed

Renaming Columns

natalie_

Community Trekker

Joined:

Jan 6, 2016

Hi everyone,

In my code, I open up a csv file, and I rename the column to "Column 1".  In the data table, it is renamed, but when I search through all the different columns, it can't find "Column 1". It only recognizes its original name when it is opened.  I am searching for values with in a data table, and I need to know what column it is in (which is working if I set i to 2).  I know that setting i to 2 would work for this application, but in general, I think it would be good to know why this is happening.

colFunction = Function({search_string},

  found =0;

  i=1;

  Show(search_string);

  Show(nCols);

  While (found == 0,

  match_rows = dt << get rows where(as column(dt, nCols)==search_string);

  if(nrows(match_rows) > 0, found = 1, i++);

  if(i==nCols, found =1, );

  );

  col = Num(i);

);

file = Pick File();

dt = Open (file);

for(i=1, i<=ncols(dt), i++,

  column(dt,i) << data type(Character);

  column(dt,i) << set modeling type(continuous);

);

nCols = dt << Get Column Names ("Character");

col = Column (1);

col << set name("Column 1");

irglCol = colFunction("Irgl");

1 REPLY
Byron_JMP

Staff

Joined:

Apr 26, 2012

get column names gets just the list of column names

get column references gets the names as strings

so maybe a couple of other ideas too.

open(); //dont have to pick the file, it automatically opens the pic file dialogue

Get the column list, find the position of the pattern and then change the column name using the position at the reference

list=dt<< get column reference();  //gets strings not a list of columns

position=contains(list, column("Carline"));    //finds the position of something in a list

column(position)<<setname("Caroline");

or I guess you could get really crazy and substitute in the list and rename all the columns with the list

newlist=substitute(list, string, "Column 1");

dt<<set column names(newlist);