Problem with nested mapping for Dataset structures

The exhibited expression does not work because the function argument a is an association. Query syntax is only recognized when it appears within the arguments of a Dataset or Query expression. We can use Query to fix up the function:

data[{All -> Function[a, a // Query[{"a" -> {"B" -> (# - a["b", "X"] &)}}]]}]

dataset screenshot

As an aside... the following expression violates the spirit of the question but it reads fairly well:

data[All, Module[{a = #}, a["a", "B"] -= a["b", "X"]; a] &]

dataset screenshot


I realized while writing this what the problem was, and thought I'd write it up just to share. The problem is this line:

This function, in turn, involves mapping a function at specific field names of a dataset: each of its arguments is the sub-dataset consisting of a single association (with keys "a", "b").

That's not true! Each argument to somefunction is the un-Dataset-ified association. It's easy to think "all subparts of Datasets are Datasets", though; after all, querying the dataset to get the first association doesn't return an Association object—it returns a Dataset! That is, data[1] has head Dataset.

Anyway, to solve it, we have to revert back to using Association tools—in this case, MapAt. we want

data[{All -> Function[a, MapAt[# - a["b", "X"] &, a, {"a", "B"}]] }]

That does the trick. However, I'd be interested if anyone knows how to do this by employing only the Dataset structure and not reverting back to acting on its component pieces—especially the functionality of combining parts of the same dataset for each element in a dataset.

(Would this be worth a feature suggestion if there is clean Dataset-only way to do it, or not?)

Tags:

Dataset