Is there a way to operate on Dataset across levels to apply a function f
to a key -> value
pair in an Association as follows:
pivotApply[f_, key_ -> value_] := key -> f[key, value]
For example, take the Titanic dataset and pivot up "class":
titanicClass = titanic[GroupBy[Key@"class"], KeyDrop[Key@"class"]];
For this problem, assume only titanicClass. "class"
key has been deleted and so can't referenced. The motivation is tree-structured data where it would be inefficient to insert the keys {"1st"...}
at the deeper level.
Here foo
represents a "client" function to be applied to deeper level data but depenent on class values.
EDIT // #age >= 18 &
foo["1st", data_] := Dataset[data][All, #age >= 18 &];
foo["2nd", data_] := Dataset[data][All, "sex"];
foo["3rd", data_] := Dataset[data][All, "survived"];
Is there a way to avoid inefficient downcasting to Normal and back to Dataset to use AssociationMap? Also note there are Datasets nested in a larger one, which should also be cast to Lists (since there is an outer Dataset).
AssociationMap[pivotApply[foo, #] &, titanicClass // Normal] // Dataset
Is there a way to query Datasets across levels like this? This seems extremely roundabout, but haven't figured an implementation using, say Keys and Values and related Dataset operators.