Function Repository Resource:

ContainsMissing

Source Notebook

Test if an expression contains a Missing value

Contributed by: Arnoud Buzing

ResourceFunction["ContainsMissing"][expr]

gives True if expr contains Missing and False otherwise at level 1 of expr.

ResourceFunction["ContainsMissing"][expr,lvl]

gives True if expr contains Missing and False otherwise at level lvl of expr.

Examples

Basic Examples (2) 

Check if an expression has a missing value at default level 1:

In[1]:=
ResourceFunction["ContainsMissing"][{1, 2, Missing["NotAvailable"]}]
Out[1]=

Check if an expression contains a missing value (at default level 1):

In[2]:=
ResourceFunction[
 "ContainsMissing"][{1, 2, {3, Missing["NotAvailable"]}}]
Out[2]=

At level 2, the expression contains a missing value:

In[3]:=
ResourceFunction[
 "ContainsMissing"][{1, 2, {3, Missing["NotAvailable"]}}, 2]
Out[3]=

Scope (4) 

Define some sample data with missing values:

In[4]:=
data = {
   {{2022, 1, 1}, {10, 10, 10, 10, 10}},
   {{2022, 1, 2}, {20, 20, 20, 20, 20}},
   {{2022, 1, 3}, {30, 30, 30, 30, Missing["NotAvailable"]}}
   };

Check if it contains missing values at the lowest level (-1):

In[5]:=
ResourceFunction["ContainsMissing"][data, -1]
Out[5]=

Use with Select to get the element of the list with the missing value:

In[6]:=
Select[data, ResourceFunction["ContainsMissing"][#, 2] &]
Out[6]=

Select the data that does not contain missing values:

In[7]:=
Select[data, Not[ResourceFunction["ContainsMissing"][#, 2]] &]
Out[7]=

Possible Issues (1) 

ContainsMissing does not yield True if only the symbol Missing is present; it yields True only if there is a Missing expression:

In[8]:=
ResourceFunction["ContainsMissing"][{{1}, {3, Missing}}, -1]
Out[8]=
In[9]:=
ResourceFunction["ContainsMissing"][{{1}, {3, Missing[]}}, -1]
Out[9]=

Publisher

Arnoud Buzing

Version History

  • 1.0.0 – 25 March 2022

License Information