12
$\begingroup$

Is there a syntax or function to reference parent and root when querying nested Associations similar to Unix' ".." and "/", eg "cd ../../mydir". ?

Here's a typical application of the root construct. Given a nested dataset:

ds = <|  "study" -> <|   "all"  -> <|"n" -> 223.`, "Median" -> 18.5` |>|>, 
        "bySite" -> <|   "VASD" -> <|"n" -> 107.`, "Median" -> 20.40`|>, 
                         "UCSD" -> <|"n" -> 116.`, "Median" -> 16.5` |>|>, 
  "bySpeciality" -> <|"Primary" -> <|"n" -> 127.`, "Median" -> 17.8` |>, 
                    "Specialty" -> <|"n" -> 96.` , "Median" -> 19.5` |>|>|> // Dataset

enter image description here

(Dataset is only used here for formatting convenience)

using ds["study","all","n"] as denominator, the query is to convert all values of the instances "n" to ratios:

   ds[All, All, #/ds["study", "all", "n"] &] 

enter image description here

But how can it be used in a query without reference to ds? For example, would like to use something like RootAssociation

ds // Query[All, All, {"n" -> (#/RootAssociation["study", "all", "n"] &)}]

ParentAssociation would work in a similar way

$\endgroup$
  • $\begingroup$ In general you'll be more likely to get responses if you use code that others can immediately evaluate, and simplify your examples as far as you can. The RightCompositions make the code harder to read and understand for someone who isn't familiar with it already. $\endgroup$ – user6014 Nov 23 '16 at 14:50
  • $\begingroup$ @user6014, re the first point, I'll replace w/ a self-contained version. Re RightComposition - I think it makes code easier to read, it's just an infix version. $\endgroup$ – alancalvitti Nov 23 '16 at 17:54
  • $\begingroup$ @user6014, I simplified the example to make it more readable. $\endgroup$ – alancalvitti Nov 23 '16 at 20:08
  • $\begingroup$ What about Function[RootAssociation, RootAssociation//Query[All, All, {"n" -> (#/RootAssociation["study", "all", "n"] &)}] ] isn't necessarily better but you can define queries separately. $\endgroup$ – Kuba Nov 23 '16 at 20:17
  • $\begingroup$ @Kuba, I would consider that cheating, as you might as well write: ds // Function[ds, ds // Query[All, All, {"n" -> (#/ds["study", "all", "n"] &)}]] - maybe a version using UpValues? $\endgroup$ – alancalvitti Nov 23 '16 at 20:24

Your Answer

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

Browse other questions tagged or ask your own question.