I'd like to filter a Dataset using a parameter in Select, but so far I haven't been able to make it work properly. Say that I have the following data:

data = {
   {"Group", "aa", "bb"}
   , {"A", 3.24, 9.68}
   , {"B", 3.5, "-"}
   , {"C", "", 6.79}

I convert my data to a dataset:

dataset = 
 Dataset[AssociationThread[First@data, #] & /@ Rest@data]

Extracting only these datasets, in which the aa data are numbers:

dataset[Select[NumberQ[#"aa"] &]]

Now I want to either select aa or bb using a parameter, but why doesn't this work?

sel = "aa"
dataset[Select[NumberQ[#sel] &]]
  • $\begingroup$ Possible dupe. This is covered here: mathematica.stackexchange.com/questions/59732/… $\endgroup$ – Gordon Coale Jan 26 '15 at 14:03
  • 1
    $\begingroup$ basically Select[#State =="New York" || #Population > 2000000 &]@cityDS (or cityAssoc) for a Dataset you can also use DS[Select[#State=="New York" &],"Population"] selecting the "Population" column of the dataset DS where the "State" column = New York. Use || for OR and && for AND logical operators $\endgroup$ – Gordon Coale Jan 26 '15 at 14:11
  • 2
    $\begingroup$ dataset[Select[NumberQ[#[sel]] &]] or dataset[Select[NumberQ[#@sel] &]] . Read the scope section of the #(Slot) docs $\endgroup$ – SquareOne Jan 26 '15 at 14:19
  • $\begingroup$ @SquareOne Thanks (again!). I did read through the #-Sektion, but couldn't get it to work. I'll certainly go through the Scope section again. – My program is finished now. Dataset is somewhat tricky to get your (i.e. my ...) head around. But it seems quite powerful once you get it. $\endgroup$ – Mockup Dungeon Jan 26 '15 at 20:21
  • $\begingroup$ @Gordon Coale, I went through the topic you identified as possible dupe, but didn't find the answer there. Thanks for your input! $\endgroup$ – Mockup Dungeon Jan 26 '15 at 20:22

The example does not work because #sel is a special shorthand notation that is interpreted as if we had written #["sel"]. Observe that sel becomes quoted as part of the expansion, meaning that the expression will attempt to select the (non-existent) value associated with the literal string "sel". This shorthand is mentioned in the Details section of the documentation for Slot.

To make the example work, we need to explicitly specify sel in unquoted form so that it will be evaluated. #[sel] is what we need:

sel = "aa";
dataset[Select[NumberQ[#[sel]] &]]

dataset screenshot

Alternatively, we could use the part notation #[[sel]]:

dataset[Select[NumberQ[#[[sel]]] &]]

dataset screenshot

Such notation is listed in the documentation for Part.


As noted by commentators, there is nothing special about the expression #[sel] that would prevent us from using prefix form (# @ sel) or postfix form (sel // #) if desired.

  • 1
    $\begingroup$ worth adding squareones second example as a third way or doing it? $\endgroup$ – Gordon Coale Jan 27 '15 at 9:10
  • $\begingroup$ @GordonCoale Done. $\endgroup$ – WReach Jan 27 '15 at 15:00

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.