It is cool that custom functions can be used as well as part of custom formulas in Formula Editor.
With the Transform Category command, the
New Custom Function( "myNamespace", "Add Ten", Function( {x, y = 10}, x + y ), << Transform Category( "Custom" ));
from https://www.jmp.com/support/help/en/16.2/#page/jmp/create-custom-functions-transforms-and-formats.sh...
will show up in the right click New Formula Column Menu - and produce a column with formula
I just started with @txnelson 's approach
https://community.jmp.com/t5/Discussions/Formula-for-Number-of-Unique-Categories-in-Column/m-p/55284...
to generate a column to Count Unique values - but instead of typing the formula, I wanted to use a Custom Function.
The manually generated Formula Column works, but the column that uses the Custom Function count unique doesn't.
It fails with the error message
Seems that x in the by() expression is not replaced with the column name.
So, use Eval(Substitute() to fix the issue?
... but how?
With
Eval(Substitute(Expr(Summarize( values = by( _col_ ) )),Expr(_col_),Name Expr(x) ));
I get
- because in a Column Formula :x is treated as :x[row()]. Argh ...
Along this line, I start to wonder why does this work:
[NB: the red zig-zag line just tells the user that print in a column formula is not a good idea]
why does Jmp know that it has to send get values to the COLUMN :sex and and not to :sex[row()].
And why does it actually return :sex[row()] in the second line - and not the column :sex ?
Names Default To Here( 1 );
dt = Open( "$SAMPLE_DATA/Big Class.jmp" );
Add Custom Functions(
New Custom Function(
"custom",
"count unique",
Function( {x},
As Constant( Summarize( values = by( x ) ) );
N Items( values );
),
<<Transform Category( "Custom" ),
<<Description( "count unique values" )
)
);
New Column( "functionColumn",
Formula(
As Constant( Summarize( values = by( :sex ) ) );
N Items( values );
)
); //works
New Column( "customFunctionColumn", Formula( custom:count unique( :sex ) ) ); //doesn't work