3
$\begingroup$

How to generate a long Select query with Dataset-based syntactic sugar? For instance, let we generate the dataset

keys="key" ~~ (ToString@#) & /@ Range[30]; 
data = Dataset@(Table[AssociationThread[keys -> RandomChoice[{0, 1, ""} , 30]], 50] )

like

enter image description here

My goal is to generate an operator selecting, for instance, only rows with known key1-key10. But no one straightforward combination works. My attempts

data[AllTrue /* Select, keys[[1 ;; 10]], ! StringQ@# &] (* does not work*)
data[Select, AllTrue, keys[[1 ;; 10]], ! StringQ@# &]  (* does not work*)

... et.c.

$\endgroup$

1 Answer 1

5
$\begingroup$

The trick is to express the selection criteria within a subquery:

data[Select[Query[keys[[1;;10]] /* AllTrue[Not@*StringQ]]]]

For example, using a smaller dataset for presentation purposes...

SeedRandom[0];
{rows, cols} = {10, 10};
keys = "key" ~~ (ToString@#) & /@ Range[cols];
data = Dataset[Table[AssociationThread[keys -> RandomChoice[{0, 1, ""}, cols]], rows]]

initial dataset

... we can query for rows where the first four columns are all not strings:

data[Select[Query[keys[[1;;4]] /* AllTrue[Not@*StringQ]]]]

filtered dataset

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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