Function Repository Resource:

NFunction

Source Notebook

Allow a function to perform pure numeric computation by silently ignoring non-numeric values

Contributed by: Ting Sun

ResourceFunction["NFunction"][fun]

makes a special operator form of fun that performs pure numeric computation by ignoring non-numeric values.

Details

ResourceFunction["NFunction"][fun] mimics the behavior of fun, but performs computations only for numeric values; all non-numeric values are ignored.
When applied to purely numeric data arrays, ResourceFunction["NFunction"][fun] and fun give identical results.
When applied to an empty data array, ResourceFunction["NFunction"][fun] gives Indeterminate.
Functions processed by ResourceFunction["NFunction"] behave similarly to numpy nan-family functions.

Examples

Basic Examples (2) 

Mean processed by NFunction will ignore non-numerical values when applied to a list:

In[1]:=
ResourceFunction["NFunction"][Mean][{1, Indeterminate, Missing[], 4}]
Out[1]=

For arrays with rank ≥ 2, NFunction is applied columnwise:

In[2]:=
ResourceFunction["NFunction"][Mean][
 Table[{j^2, Indeterminate, Missing[], RandomReal[]}, {j, 4}]]
Out[2]=

Scope (6) 

Custom functions can be processed by NFunction:

In[3]:=
fun[x_] := Median[x]/Mean[x];
ResourceFunction["NFunction"][fun]@
 Table[{j^2, Indeterminate, Missing[], RandomReal[]}, {j, 4}]
Out[3]=

Functions processed by NFunction can work with Dataset:

In[4]:=
ExampleData[{"Dataset", "Titanic"}][
 ResourceFunction["NFunction"][Median]]
Out[4]=

Functions processed by NFunction can work with Quantity:

In[5]:=
ResourceFunction["NFunction"][Mean][{Missing[], Indeterminate, Quantity[3, "Meters"], Quantity[3.4, "Meters"], Quantity[none, "Meters"]}]
Out[5]=

Functions processed by NFunction can work with Around:

In[6]:=
ResourceFunction["NFunction"][MeanAround][{Around[3, 2], Indeterminate, Around[3, Indeterminate], Around[3, 12], Around[3, .2]}]
Out[6]=

Functions that produce results similar to List can also be processed by NFunction:

In[7]:=
ResourceFunction["NFunction"][Quartiles][
 Table[{j^2, Indeterminate, Missing[], RandomReal[]}, {j, 3}]]
Out[7]=

Non-numeric results are given as Indeterminate:

In[8]:=
ResourceFunction["NFunction"][Mean][{}]
Out[8]=

Properties and Relations (1) 

Functions processed by NFunction give the same results as those without processing when applied to numeric data arrays:

In[9]:=
array = RandomReal[10, {3, 5}];
Median[array] === ResourceFunction["NFunction"][Median][array]
Out[10]=

Possible Issues (1) 

Some functions require a minimum number of valid values:

In[11]:=
ResourceFunction["NFunction"][Skewness][{3, Missing[]}]
Out[11]=

Publisher

Ting Sun

Version History

  • 4.0.0 – 07 September 2021

Related Resources

Author Notes

: add support for functions that produce List-like results.

License Information