Hi All,
How to access the contents of a column by the column reference? Since my column names keep changing with every loop in the script, I can't just simple use the column name to subtract.
dt is my data table and has 30 columns. I want to create a new column with dt[28]- dt[30].
Can anyone help me with syntax?
I really appreciate it. Thank you.
This example shows how variables can be used for defining columns dynamically when 1) using the matrix notation for tables in JMP 13 or 2) setting a formula.
dt = New Table("Table Subscripting",
New Column("a", Set Values([1 2 3 4 5])),
New Column("b", Set Values([44 22 77 55 99])),
New Column("c", Set Values([59, 58, 57, 56, 55]))
);
startCol = 1;
endCol = N Col(dt);
// New column with the diff between endCol and startCol can be filled
// 1) with static values using matrix notation (JMP 13),
dt << New Column("Delta");
dt[0, endCol + 1] = dt[0, endCol] - dt[0, startCol];
// 2) or by a Column Formula
// (expression must partly be pre-evaluated if variables are used)
F = Eval Expr(Expr(Column(endCol))[] - Expr(Column(startCol))[]);
dt << New Column("Delta2", Formula(Name Expr(F)));
There is more than one way to refer to a column:
I suspect that the last two forms based on the Column() function will be useful in your case.
Additionally, if you're running JMP 13, you can subscript into a data table using dt[row, column], and you can use ranges (1::4).
Here's a short example that adds a new column and sets its values to column 3 - columnn 1:
dt = New Table( "Table Subscripting",
New Column( "a", Set Values( [1 2 3 4 5] ) ),
New Column( "b", Set Values( [44 22 77 55 99] ) ),
New Column( "c", Set Values( [59, 58, 57, 56, 55] ) )
);
dt << New Column( "new column" );
dt[1::nrow(dt), 4] = dt[1::nrow(dt), 3] - dt[1::nrow(dt), 1];
This example shows how variables can be used for defining columns dynamically when 1) using the matrix notation for tables in JMP 13 or 2) setting a formula.
dt = New Table("Table Subscripting",
New Column("a", Set Values([1 2 3 4 5])),
New Column("b", Set Values([44 22 77 55 99])),
New Column("c", Set Values([59, 58, 57, 56, 55]))
);
startCol = 1;
endCol = N Col(dt);
// New column with the diff between endCol and startCol can be filled
// 1) with static values using matrix notation (JMP 13),
dt << New Column("Delta");
dt[0, endCol + 1] = dt[0, endCol] - dt[0, startCol];
// 2) or by a Column Formula
// (expression must partly be pre-evaluated if variables are used)
F = Eval Expr(Expr(Column(endCol))[] - Expr(Column(startCol))[]);
dt << New Column("Delta2", Formula(Name Expr(F)));