Create and populates new columns based categories in an existing columnHello,
I'm trying to build a quick query in which a categorical column (test_article) is used to create a series of column which contains only a subset of the information. If the script works as intended, the table would end up with 2 new columns (PBS vs ...).
| test_article | measurement | PBS vs 709547 | PBS vs 709139 |
| PBS | 1.23 | PBS | PBS |
| PBS | 1.456 | PBS | PBS |
| 709547 | 4.56 | 709547 | EXCLUDE |
709139 | 5.02 | EXCLUDE | 709139 |
| 709139 | 6.02 | EXCLUDE | 709139 |
| 709547 | 4.57 | 709547 | EXCLUDE |
I've tried the following script:
NamesDefaultToHere(1);
dt=current data table();
row = Associative Array( :test_article ) << Get Keys;
nc=N items (row);
For( i = 1, i <= nc, i++,
new column("PBS vs "|| row[i],"Character", "Nominal", Formula(If( :test_article == "PBS", :test_article,
If( :test_article == row[i],
"test",
"EXCLUDE"
)
))));Somehow, it does not populate any values in the new columns with the exception of 'PBS'.
Any suggestions?
Thanks a lot
Sebastien
Re: Create and populates new columns based categories in an existing columnAlso works like a charm.
Thanks a lotRe: Create and populates new columns based categories in an existing columnWorks like a charm!
ThanksRe: Create and populates new columns based categories in an existing columnYou need to get the value of row[i] into the formula. JMP doesn't evaluate expressions inside the Formula() part of the New Column() message.
You can use Substitute() to get the actual value of row[i] into the formula.
Names Default To Here( 1 );
dt = Current Data Table();
row = Associative Array( :test_article ) << Get Keys;
nc = N Items( row );
For( i = 1, i <= nc, i++,
Eval(
Substitute(
Expr(
New Column( "PBS vs " || row[i],
"Character",
"Nominal",
Formula( If( :test_article == "PBS" | :test_article == ri, :test_article, "EXCLUDE" ) )
)
),
Expr( ri ), Eval( row[i] )
)
)
);
Re: Create and populates new columns based categories in an existing columnThe primary issue you are having is that within the definition of a formula, one can not have items that need to be evaluated. JMP thinks everything within the formula definition is to be evaluated only when the formula is executed.
Therefore, the formula that is passed into the New Column definition, must be complete in it's syntax. The modification of your code below works with your sample data table.
Names Default To Here( 1 );
dt = Current Data Table();
row = Associative Array( :test_article ) << Get Keys;
nc = N Items( row );
For( i = 1, i <= nc, i++,
Eval(
Parse(
"New Column( \!"PBS vs " || row[i] ||
"\!",
Character,
Nominal,
Formula(
if (Row() == 1, theTest = \!""
|| row[i] ||
"\!");
If( :test_article == \!"PBS\!",
zip=:test_article,
If( :test_article == theTest,
zip=\!"test\!",
zip=\!"EXCLUDE\!"
)
);
zip
)
)
);"
)
)
);
