Subscribe Bookmark RSS Feed

Column formula referencing variable column names

jeric

Community Trekker

Joined:

Nov 1, 2014


Greetings,

For analyzing electrical data I want to create new columns with an absolute value formula referring to other columns whose names may be different in different data sets but start with "ID" or "IG".   When I apply below the formula for every new column is the absolute value using the last measurement column found, not the column corresponding to different measurement columns.  How should I express that each "ABS" formula applies to the measurement column?

cdt = Current Data Table();

ColNames = {};

ColNames=cdt<< Get Column Names(string);

for (i=1, i<= N items(ColNames), i++,

if (Substr(ColNames ,1,2) == "ID" |

     Substr(ColNames ,1,2)== "IG" ,

    (

      col = Column(i);

      New Column("ABS " || ColNames , (Formula(Abs(As Column (col)))), format("Scientific", 6, 1));

1 ACCEPTED SOLUTION

Accepted Solutions
Solution

NamesDefaultToHere(1);

// Make a table . . .

dt = NewTable("ETEST",

  NewColumn("ID_1", Numeric, Continuous, Formula(RandomNormal())),

  NewColumn("ID_2", Numeric, Continuous, Formula(RandomNormal())),

  NewColumn("Not Required", Numeric, Continuous, Formula(RandomNormal())),

  NewColumn("IG_1", Numeric, Continuous, Formula(RandomNormal())),

  NewColumn("IG_2", Numeric, Continuous, Formula(RandomNormal())),

  AddRows(10)

);

// Add new formula columns as required . . .

cols = dt << GetColumnNames("String");

Wait(3);

For(c=1, c<=NItems(cols), c++,

  If(StartsWith(cols[c], "ID") | StartsWith(cols[c], "IG"),

  // Template expression for adding the new column by a formula

  CMD = Expr( dt << NewColumn("ABS("||cols[c]||")", Numeric, Continuous, Formula(Abs(colTBD))) );

  // Update the expression with values for this iteration . . .

  SubstituteInto(CMD, Expr(colTBD), AsName(cols[c]));

  // Add the required column

  CMD;

  );

);

2 REPLIES
Solution

NamesDefaultToHere(1);

// Make a table . . .

dt = NewTable("ETEST",

  NewColumn("ID_1", Numeric, Continuous, Formula(RandomNormal())),

  NewColumn("ID_2", Numeric, Continuous, Formula(RandomNormal())),

  NewColumn("Not Required", Numeric, Continuous, Formula(RandomNormal())),

  NewColumn("IG_1", Numeric, Continuous, Formula(RandomNormal())),

  NewColumn("IG_2", Numeric, Continuous, Formula(RandomNormal())),

  AddRows(10)

);

// Add new formula columns as required . . .

cols = dt << GetColumnNames("String");

Wait(3);

For(c=1, c<=NItems(cols), c++,

  If(StartsWith(cols[c], "ID") | StartsWith(cols[c], "IG"),

  // Template expression for adding the new column by a formula

  CMD = Expr( dt << NewColumn("ABS("||cols[c]||")", Numeric, Continuous, Formula(Abs(colTBD))) );

  // Update the expression with values for this iteration . . .

  SubstituteInto(CMD, Expr(colTBD), AsName(cols[c]));

  // Add the required column

  CMD;

  );

);

jeric

Community Trekker

Joined:

Nov 1, 2014

Works like a charm.  Thank you, sir!