6
$\begingroup$

Mathematica has almost the following example in the documentation:

dataset = Dataset[{<|"a" -> 1, "b" -> "x", "c" -> 7|>, <|"a" -> 2, "b" -> "y","c" -> 9|>, 
 <|"a" -> "-", "b" -> "z", "c" -> "-"|>, <|"a" -> 4, "b" -> "x", "c" -> 1|>,
 <|"a" -> "-", "b" -> "y", "c" -> "-"|>, <| "a" -> 6, "b" -> "z", "c" -> 8|>}]

The first and last columns are slightly changed compared to the Mathematica documentation. To select data based on a criteria related to a column the example is:

dataset[Select[#a < 5 &]] 

What I need to have is – in its simple form:

dataset[Select[
  NumberQ[#["a"]] || NumberQ[#["b"]] || NumberQ[#["c"]] &]]

However, in the complete form there are 300+ columns, and I want to select all the datasets – but only from a subset of columns (i.e. categories/keys) – that contain numbers. I have the categories of the columns to which Select should apply in a list (in this case the list would be {"a","b","c"}).

$\endgroup$
  • $\begingroup$ Could you do something like: dataset[Select[Or @@ (NumberQ /@ Slot /@ {"a", "b", "c", "d"})]]? I'm not sure I totally grasp your example; I hadn't seen Dataset used like this before, but it's cool! $\endgroup$ – Ben Kalziqi Oct 18 '17 at 20:55
2
$\begingroup$

You said that your dataset had over 300 columns, so perhaps the following dataset is a bit more representative:

SeedRandom[2];
dataset = Dataset @ Table[
    AssociationThread[{"a","b","c","d","e"}, RandomChoice[Range[2]~Join~{"-",u,v,w,x,y,z}, 5]],
    {10}
]

enter image description here

Then, if you want to select rows where there's a number in columns a, b or c, you can do the following:

With[{slots = Slot/@{"a","b","c"}},
    dataset[Select[AnyTrue[slots, NumberQ]&]]
]

enter image description here

$\endgroup$
  • $\begingroup$ After playing around with the first solution, I came to realise this one is what I actually need, as I need to select the columns/categories in which to test this. Beautiful, thanks! $\endgroup$ – Mockup Dungeon Oct 20 '17 at 7:46
3
$\begingroup$
Select[AnyTrue  @ NumberQ] @ dataset

enter image description here

$\endgroup$
0
$\begingroup$

I think you are asking for:

Select[NumberQ[#"a"] || NumberQ[#"b"] || NumberQ[#"c"] &]@dataset[All, {"a", "b", "c"}]
$\endgroup$
0
$\begingroup$

This may help

    Select[NumberQ[#a] & || NumberQ[#b] & || NumberQ[#c] &]@dataset
$\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.