groups

Syntax

groups(X, [mode=’dict’])

Arguments

X is a vector.

mode is an optional parameter indicating the data form returned by the function. The default value is “dict”. It can be specified as:

  • “dict”: return a dictionary. The key of the dictionary stores the unique value in X; the value is a vector that stores the indices of all elements that hold the value.

  • “table”: return a table with 2 columns, “key” and “index”, storing the unique value in X and the corresponding indices.

  • “vector”: return an array vector. The elements are the indices of each unique value in X, sorted in ascending order.

  • “tuple”: return a tuple. The elements are stored the same as mode=”vector”.

Details

For each unique value in X, return the indices of all elements that hold the value.

If mode = “dict”, return a dictionary.

If mode = “table”, return a table with 2 columns, key and index. Each cell in the column index is an array vector.

Examples

$ x=NULL NULL 12 15 12 16 15 14 NULL NULL
$ groups(x);

16->[5]
->[0,1,8,9]
12->[2,4]
14->[7]
15->[3,6]

$ groups(x, "vector")
[[0,1,8,9],[2,4],[7],[3,6],[5]]

$ groups(x, "tuple")
([0,1,8,9],[2,4],[7],[3,6],[5])

$ groups(x, "table")

key

index

[0,1,8]

2

[2,4]

4

[7]

5

[3,6]

6

[5]