Categories &

Functions List

Function Reference: grp2idx

statistics: g = grp2idx (s)
statistics: [g, gn] = grp2idx (s)
statistics: [g, gn, gl] = grp2idx (s)

Get index for grouping variable.

g = grp2idx (s) returns a numeric column vector of integer values g indexing the distinct groups in the grouping variable s. s can specified as any of the following data types:

  • categorical vector
  • cell array of character vectors
  • character array
  • duration vector
  • logical vector
  • numeric vector

s must be a vector, unless it is a 2-D character array. In the case of numerical and logical data types, the group indices are ordered in sorted order of s. In the case of categorical arrays, the group indices are allocated by the order of the categories in s. For the rest of the data types, the group indices are allocated by order of first appearance in s. Note that in case of a categorical grouping variable, the indexing integer values might not be continuous, since s may contain unassigned categories. For every other data type, g will contain integer values in the the range [1:K], where K is the number of distinct groups in s.

[g, gn] = grp2idx (s) also retuns a cell array of character vectors gn representing the list of group names. The order of the group names in gn follow the same pattern as the group indices in g according to the data type of s, as described above.

[g, gn, gl] = grp2idx (s) further returns a column vector gl representing the list of the group levels with the same data type as s.

Note that standard missing values in s appear as NaN in g and are not present on either gn and gl.

See also: grpstats

Source Code: grp2idx