```help mata mm_freq()
-------------------------------------------------------------------------------

Title

mm_freq() -- Compute frequency counts

Syntax

real colvector mm_freq(x [, w, levels])

real colvector _mm_freq(x [, w, levels])

real colvector mm_freq2(x [, w])

real colvector _mm_freq2(x [, w])

where

x:  transmorphic matrix containing data, rows are observations

w:  real colvector containing weights

levels:  transmorphic matrix containing levels of data, in ascending
order

Description

mm_freq() returns the frequency counts of the "levels" (unique rows) of
x. That is, mm_freq() computes the frequency distribution of x. The
frequency counts are returned in ascending sort order of the levels of x.

w specifies weights associated with the observations in x. Omit w, or
specify w as 1 to obtain unweighted results.

levels specifies the levels to be counted. levels must be sorted in
ascending order. If levels is omitted, mm_freq() determines the frequency
counts of all levels of x. Furthermore, levels==. indicates to determine
the counts of all levels and, additionally, return the levels of x in
levels.

mm_freq2() returns for each row of x the number of duplicates in x (or
the sum of weights associated with the duplicates, respectively).

_mm_freq() and _mm_freq2() are like mm_freq() and mm_freq2(), but assume
that x is sorted.

Remarks

Example:

: x = (1\2\3\.\1\3\1\1\2\3\.)

: mm_freq(x,1)
1
+-----+
1 |  4  |
2 |  2  |
3 |  3  |
4 |  2  |
+-----+

: x, mm_freq2(x,1)
1   2
+---------+
1 |  1   4  |
2 |  2   2  |
3 |  3   3  |
4 |  .   2  |
5 |  1   4  |
6 |  3   3  |
7 |  1   4  |
8 |  1   4  |
9 |  2   2  |
10 |  3   3  |
11 |  .   2  |
+---------+

: l = (0\1\2)

: f = mm_freq(x,1,l)

: l, f
1   2
+---------+
1 |  0   0  |
2 |  1   4  |
3 |  2   2  |
+---------+

Note that

: f = mm_freq(x,1,l=.)

: l, f
1   2
+---------+
1 |  1   4  |
2 |  2   2  |
3 |  3   3  |
4 |  .   2  |
+---------+

is equivalent to

: l = uniqrows(x)

: f = mm_freq(x,1,l)

: l,f
1   2
+---------+
1 |  1   4  |
2 |  2   2  |
3 |  3   3  |
4 |  .   2  |
+---------+

Furthermore, mm_freq() may also be used with strings:

: s = ("a"\"b"\"c"\"b")

: mm_freq(s,1)
1
+-----+
1 |  1  |
2 |  2  |
3 |  1  |
+-----+

Conformability

mm_freq(x, w, levels), _mm_freq(x, w, levels):
x:  n x c
w:  n x 1 or 1 x 1
levels:  l x c or levels==.
result:  l x 1.

mm_freq2(x, w), _mm_freq2(x, w):
x:  n x c
w:  n x 1 or 1 x 1
result:  n x 1.

Diagnostics

None.

Source code

mm_freq.mata

Author

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

Also see

Online:  help for [M-5] uniqrows(), [M-4] utility, mm_levelsof(),
moremata
```