help mata mm_nunique()
-------------------------------------------------------------------------------

Title

mm_nunique() -- Count number of distinct values

Syntax

real scalar mm_nunique(transmorphic vector X)

real scalar mm_nuniqrows(transmorphic matrix Z)

Description

mm_nunique() returns the number of distinct values in vector X.

mm_nuniqrows() returns the number of unique rows in matrix X. mm_nuniqrows(Z) is equivalent to rows(uniqrows(Z)), but it is faster and uses less memory.

Remarks

Examples:

: X 1 2 3 4 +-----------------+ 1 | 1 2 3 1 | +-----------------+

: mm_nunique(X) 3

: Z 1 2 +---------+ 1 | 1 1 | 2 | 2 2 | 3 | 3 3 | 4 | 1 1 | +---------+

: mm_nuniqrows(Z) 3

Note that mm_nunique() is faster than mm_nuniqrows() but can only be applied to vectors. Furthermore, if the data vector is already sorted, mm_npanels() may be used which is faster than mm_nunique() .

Conformability

mm_nunique(X) X: r x 1 or 1 x c result: 1 x 1

mm_nuniqrows(Z) Z: r x c result: 1 x 1

Diagnostics

mm_nunique() returns 0 if length(X)==0.

mm_nuniqrows() returns 0 if rows(Z)==0 and returns 1 if rows(Z)>0 and cols(Z)==0.

Source code

mm_nunique.mata

Author

Ben Jann, ETH Zurich, jann@soz.gess.ethz.ch

Also see

Online: help for [M-5] uniqrows(), mm_panels(), moremata