Skip to main content

Normal // Association // Dataset workaround for some user query operators


Given example tabular data (ie, with rows and columns, or "data frames")



data = Table[<| "key1" -> i, "key2" -> i + 10 , 
"key3" -> i + 20|>, {i, 4}] //Dataset

enter image description here


The following query operator almost works to associate (the values of) a selected "primary" key at first level:


primaryKey[key_] := Sequence[All, Slot[key] -> KeyDrop[#, key] &] 

This allows the lookup syntax:


data[primaryKey["key1"]][3]


(* 3 -> <|"key2" -> 13, "key3" -> 23|> *)

How to modify this query to handle:




  1. Return only the value <|"key2" -> 13, "key3" -> 23|>, similar to how data[All, #key1 &] // Normal returns only the values {1, 2, 3, 4}.




  2. Dataset doesn't recognize the implicit global association structure, flattening the data by 1 level:


    data[primaryKey["key1"]]





enter image description here


The following workaround works but how to incorporate in the operator?


data[primaryKey["key1"]] // Normal // Association // Dataset

enter image description here


The same issue arises from other restructuring operators, like generating an association from (the values of) two specified keys eg


data[All, #key1 -> #key3 &][2]


(* 2 -> 22 *)

Whereas


data[All, #key1 -> #key3 &] // Normal // Association // 
Dataset // #[2] &

(* 22 *)

Answer



Changing the descending operator All to the ascending Association seems to work at turning the resulting list of rules into an association.


primaryKey[key_] := Sequence[Association, Slot[key] -> KeyDrop[#, key] &] 

Comments