2
$\begingroup$

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]]&]]]

enter image description here

but where I do not have to pre define valuesOfA.

$\endgroup$
  • $\begingroup$ How do you know how many i there are ? How do you choose the value of i ? $\endgroup$ – SquareOne Nov 22 '17 at 20:43
  • $\begingroup$ @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 $\endgroup$ – SumNeuron Nov 23 '17 at 7:44
  • $\begingroup$ 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 ? $\endgroup$ – SquareOne Nov 23 '17 at 8:58
  • $\begingroup$ @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. $\endgroup$ – SumNeuron Nov 23 '17 at 9:10
  • $\begingroup$ The only solution i see is ds[GroupBy[#a &]] $\endgroup$ – SquareOne Nov 23 '17 at 9:29
2
$\begingroup$

I think you can use:

GroupBy[ds, First][[2]]

enter image description here

$\endgroup$
0
$\begingroup$

Maybe this?

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.