Main Content

ismissing

Find missing values

Description

TF = ismissing(A) returns a logical array that indicates which elements of the input data contain standard missing values. The size of TF is the same as the size of A.

Standard missing values are defined according to the data type of A, or if A is a table, the data type of each variable:

  • NaNdouble, single, duration, and calendarDuration

  • NaTdatetime

  • <missing>string

  • <undefined>categorical

  • {''}cell of character vectors

For data types with no default definition of a standard missing value, ismissing(A) returns an array or table of logical 0 (false) values the same size as A.

You can use ismissing functionality interactively by adding the Clean Missing Data task to a live script.

example

TF = ismissing(A,indicator) treats the values in indicator as missing value indicators, ignoring all default indicators listed in the previous syntax. indicator can be a single indicator or multiple indicators. For example, if A is an array of type double, then ismissing(A,[0 -99]) treats 0 and –99 as missing numeric values instead of NaN.

example

TF = ismissing(___,OutputFormat=format) specifies the output type for tabular data for any of the previous syntaxes.

Examples

collapse all

Create a row vector A that contains NaN values, and identify their locations in A.

A = [3 NaN 5 6 7 NaN NaN 9];
TF = ismissing(A)
TF = 1×8 logical array

   0   1   0   0   0   1   1   0

Create a table with variables of different data types.

dblVar = [NaN; 2; 3; 4; 5; 6];
singleVar = single([1; NaN; 3; 4; 5; 6]);
cellstrVar = {'one'; 'two'; ''; 'four'; 'five'; 'six'};
categoryVar = categorical(["red"; "orange"; "yellow"; ""; "blue"; "indigo"]);
dateVar = [datetime(2015,1:4,15) NaT datetime(2015,6,15)]';
stringVar = ["a"; "b"; "c"; "d"; "e"; missing];

A = table(dblVar,singleVar,cellstrVar,categoryVar,dateVar,stringVar)
A=6×6 table
    dblVar    singleVar    cellstrVar    categoryVar      dateVar      stringVar
    ______    _________    __________    ___________    ___________    _________

     NaN           1       {'one'   }    red            15-Jan-2015    "a"      
       2         NaN       {'two'   }    orange         15-Feb-2015    "b"      
       3           3       {0×0 char}    yellow         15-Mar-2015    "c"      
       4           4       {'four'  }    <undefined>    15-Apr-2015    "d"      
       5           5       {'five'  }    blue                   NaT    "e"      
       6           6       {'six'   }    indigo         15-Jun-2015    <missing>

Find the elements with missing values.

ismissing returns an array that has a logical 1 for every corresponding element in A with a missing value. The size of TF is the same as the size of A.

TF = ismissing(A)
TF = 6×6 logical array

   1   0   0   0   0   0
   0   1   0   0   0   0
   0   0   1   0   0   0
   0   0   0   1   0   0
   0   0   0   0   1   0
   0   0   0   0   0   1

Create a table where 'NA', '', <missing>, -99, NaN, and Inf represent missing values. Then, find the elements with missing values.

dblVar = [NaN; 3; Inf; 7; 9];
int8Var = int8([1; 3; 5; 7; -99]);
cellstrVar = {'one'; 'three'; ''; 'NA'; 'nine'};
stringVar = ["A"; "C"; "E"; missing; "I"];

A = table(dblVar,int8Var,cellstrVar,stringVar)
A=5×4 table
    dblVar    int8Var    cellstrVar    stringVar
    ______    _______    __________    _________

     NaN          1      {'one'   }    "A"      
       3          3      {'three' }    "C"      
     Inf          5      {0×0 char}    "E"      
       7          7      {'NA'    }    <missing>
       9        -99      {'nine'  }    "I"      

Specify the missing value indicators. ismissing returns an array that has a logical 1 for every corresponding element in A with a missing value.

id = {'NA' '' string(missing) -99 NaN Inf};
TF = ismissing(A,id)
TF = 5×4 logical array

   1   0   0   0
   0   0   0   0
   1   0   1   0
   0   0   1   1
   0   1   0   0

ismissing ignores trailing white space in character arrays. Therefore, because the empty character vector, '', is specified as a missing value indicator, ismissing identifies the empty character vector in A.cellstrVar as a missing value.

Input Arguments

collapse all

Input data, specified as a vector, matrix, multidimensional array, cell array of character vectors, table, or timetable.

  • If A is a timetable, then ismissing operates on the table data only and ignores NaT and NaN values in the vector of row times.

  • If A is a cell array or a table with cell array variables, then ismissing only detects missing elements when the cell array contains character vectors.

Data Types: single | double | char | string | table | timetable | cell | categorical | datetime | duration | calendarDuration

Nonstandard missing value indicator, specified as a scalar, vector, or cell array of values that the ismissing function should treat as missing.

  • If A is an array, then indicator must be a scalar or vector.

  • If A is a table or timetable, then indicator can also be a cell array with entries of multiple data types.

If you do not specify indicator, ismissing uses standard missing value indicators defined according to the data type of A:

  • NaNdouble, single, duration, and calendarDuration

  • NaTdatetime

  • <missing>string

  • <undefined>categorical

  • {''}cell of character vectors

The entries of indicator override all standard missing value indicators. To specify additional indicators while maintaining the standard list, include missing as an element in indicator.

Each indicator is used to identify missing elements in A of the same class. These indicators are also matched to elements in A of different classes:

  • double indicators also match numeric entries of A.

  • string, and char, and cell arrays of character vectors indicators also match categorical and string entries of A.

  • single, integer, and logical indicators also match double entries of A.

Example: TF = ismissing(A,0) recognizes only 0 as a missing value.

Example: TF = ismissing(A,["Unset" missing]), for categorical array A, recognizes Unset as a missing value in addition to the standard missing value for a categorical value.

Example: TF = ismissing(T,{-99 missing}), for table T, recognizes -99 as a missing value in addition to the standard missing value for the type of each table variable.

Output type for tabular data, specified as one of these values:

  • "logical" — For table or timetable input data, return the output TF as a logical array.

  • "tabular" — For table input data, return the output TF as a table. For timetable input data, return the output TF as a timetable.

For array input data, specifying the output type is not supported.

Example: ismissing(T,OutputFormat="tabular")

Output Arguments

collapse all

Missing data indicator, returned as a vector, matrix, multidimensional array, table, or timetable. The value 1 (true) corresponds to filled entries in F that were previously missing. The value 0 (false) corresponds to unchanged entries.

For array input data, TF is a logical array with the same size as A. For tabular input data, format specifies whether TF is a logical array or table or timetable with logical variables.

Data Types: logical

Tips

  • Because integer variables cannot store NaN, use a special integer value (otherwise unused) to indicate missing integer data, such as -99.

  • For more information on finding missing strings, see Test for Empty Strings and Missing Values.

  • For input data that is a structure array or a cell array of non-character vectors, ismissing returns false. To find missing values in a structure array, apply ismissing to each field in the structure by using the structfun function. To find missing values in a cell array of non-character vectors, apply ismissing to each cell in the cell array by using the cellfun function.

Algorithms

ismissing handles leading and trailing white space differently for indicators that are cell arrays of character vectors, character arrays, or categorical arrays.

  • For cell arrays of character vectors, ismissing does not ignore indicator white space. All character vectors must match exactly.

  • For character arrays in table variables, ismissing ignores trailing white space in the indicator.

  • For categorical arrays, ismissing ignores leading and trailing white space in the indicator.

Alternative Functionality

Live Editor Task

You can use ismissing functionality interactively by adding the Clean Missing Data task to a live script.

Clean Missing Data task in the Live Editor

Extended Capabilities

expand all

Version History

Introduced in R2013b

expand all