# Dataset: dynamically selecting all rows of with the nth element of a column

Suppose you have a dataset with the following layout:

ds=Dataset[{<|"a" -> "x", "b" -> 1|>,
<|"a" -> "x", "b" -> 1|>,
<|"a" -> "x", "b" -> 1|>,
<|"a" -> "y", "b" -> 1|>,
<|"a" -> "y", "b" -> 1|>,
<|"a" -> "z", "b" -> 1|>}]


how can I select all the rows where column 1 ("a") is of the the i-th value (e.g. i=2 --> "y"), e.g. as if I were to be iterating on the list produced by

valuesOfA = ds[All, "a"] // Normal // DeleteDuplicates


{"x", "y", "z"}

e.g. I am looking for something like:

With[{i = 2}, ds[Select[#a==valuesOfA[[i]]&]]]


but where I do not have to pre define valuesOfA.

• How do you know how many i there are ? How do you choose the value of i ? Nov 22 '17 at 20:43
• @SquareOne assume something like iterating via Table, e.g. Table[ds[Select#a==valuesOfA[[i]]&]],{i,Length@valuesOfA}] but again having valuesOfA be somehow innately defined Nov 23 '17 at 7:44
• Sorry i don't get it. In your post, you say you don't want to predefine valuesOfA, but how do you know how many values of A there are ? In your comment you say it should be somehow defined. Why can't you set valuesOfA = ds[All, "a"] // Normal // DeleteDuplicates ? Nov 23 '17 at 8:58
• @SquareOne the question is in regards to if there is a builtin selector or iterator so one can iterate over the unique values in a dataset without having to hard code so much. Nov 23 '17 at 9:10
• The only solution i see is ds[GroupBy[#a &]] Nov 23 '17 at 9:29

GroupBy[ds, First][[2]]

ds[GroupBy[#["a"] &], Length][Select[# == 2 &]]