Subscribe Bookmark RSS Feed

how to creat new column if same name column does not exist

robust1972

Community Trekker

Joined:

Jan 15, 2014

hi,

I need to use few columns to do calculation. the  question I have is how to create new column and set value of each cell to 0 if same name column does not exist, it there is the column, do nothing. I tried below but it does not work the column already exist. can someone helps me out?


=Data Table( "FinalBiningTable" ) << Get Column Names();

(Column_list);

((contains(Column_list, "N(P, F)"))==0, Data Table( "FinalBiningTable" ) << New Column( "N(P, F)",numeric, ordinal );:Name("N(P, F)") << Set Each Value(0);, empty());

1 ACCEPTED SOLUTION

Accepted Solutions
pmroz

Super User

Joined:

Jun 23, 2011

Solution

//dt = data table("FinalBiningTable");  // Uncomment for your table

dt = open( "$sample_data\Big Class.jmp" );

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

if (!contains(Column_list, "N(P, F)"),

      dt << New Column( "N(P, F)",numeric, ordinal, << Set Each Value(0));

);

3 REPLIES
pmroz

Super User

Joined:

Jun 23, 2011

Solution

//dt = data table("FinalBiningTable");  // Uncomment for your table

dt = open( "$sample_data\Big Class.jmp" );

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

if (!contains(Column_list, "N(P, F)"),

      dt << New Column( "N(P, F)",numeric, ordinal, << Set Each Value(0));

);

robust1972

Community Trekker

Joined:

Jan 15, 2014

Thanks! PMroz

What is wrong in my lines of code?

pmroz

Super User

Joined:

Jun 23, 2011

The default data type for get column names is a list of columns (of column data type).  When you add the string argument you get a list of column names as strings.  You were trying to compare a column data type to a string.