Note: the editor warns that this is a "subjective" post, but I am looking for an objective answer (e.g. in relation to easy of use of the many built-in functions, speed, etc).
Suppose you have a dataset which contains some hierarchical form and many other values which could be grouped by for analysis (copy paste into a notebook):
ds=Dataset[
<|"Nest1_1" -> <|"Nest2_1" ->
<|
"Nest3_1" ->
<|
<|"Nest4_1" -> <|"a" -> 1, "b" -> 4, "c" -> 7|>|>,
<|"Nest4_2" -> <|"a" -> 1, "b" -> 4, "c" -> 7|>|>
|>,
"Nest3_2" ->
<|
<|"Nest4_1" -> <|"a" -> 1, "b" -> 4, "c" -> 7|>|>,
<|"Nest4_2" -> <|"a" -> 1, "b" -> 4,
"c" -> 7|>|>
|>
|>,
"Nest2_2" ->
<|
"Nest3_1" ->
<|
<|"Nest4_1" -> <|"a" -> 1, "b" -> 4, "c" -> 7|>|>,
<|"Nest4_2" -> <|"a" -> 1, "b" -> 4, "c" -> 7|>|>
|>,
"Nest3_2" ->
<|
<|"Nest4_1" -> <|"a" -> 1, "b" -> 4, "c" -> 7|>|>,
<|"Nest4_2" -> <|"a" -> 1, "b" -> 4,
"c" -> 7|>|>
|>
|>
|>,
"Nest1_2" -> <|"Nest2_1" ->
<|
"Nest3_1" ->
<|
<|"Nest4_1" -> <|"a" -> 1, "b" -> 4, "c" -> 7|>|>,
<|"Nest4_2" -> <|"a" -> 1, "b" -> 4, "c" -> 7|>|>
|>,
"Nest3_2" ->
<|
<|"Nest4_1" -> <|"a" -> 1, "b" -> 4, "c" -> 7|>|>,
<|"Nest4_2" -> <|"a" -> 1, "b" -> 4,
"c" -> 7|>|>
|>
|>,
"Nest2_2" ->
<|
"Nest3_1" ->
<|
<|"Nest4_1" -> <|"a" -> 1, "b" -> 4, "c" -> 7|>|>,
<|"Nest4_2" -> <|"a" -> 1, "b" -> 4, "c" -> 7|>|>
|>,
"Nest3_2" ->
<|
<|"Nest4_1" -> <|"a" -> 1, "b" -> 4, "c" -> 7|>|>,
<|"Nest4_2" -> <|"a" -> 1, "b" -> 4, "c" -> 7|>|>
|>,
"Nest3_3" ->
<|
<|"Nest4_1" -> <|"a" -> 1, "b" -> 4, "c" -> 7|>|>,
<|"Nest4_2" -> <|"a" -> 1, "b" -> 4,
"c" -> 7|>|>
|>
|>
|>
|>]
which could also be flattened like:
Dataset[{
<|"Nest1" -> 1, "Nest2" -> 1, "Nest3" -> 1, "Nest4" -> 1, "a" -> 1,
"b" -> 4, "c" -> 7|>,
<|"Nest1" -> 1, "Nest2" -> 1, "Nest3" -> 1, "Nest4" -> 2, "a" -> 1,
"b" -> 4, "c" -> 7|>,
<|"Nest1" -> 1, "Nest2" -> 1, "Nest3" -> 2, "Nest4" -> 1, "a" -> 1,
"b" -> 4, "c" -> 7|>,
<|"Nest1" -> 1, "Nest2" -> 1, "Nest3" -> 2, "Nest4" -> 2, "a" -> 1,
"b" -> 4, "c" -> 7|>,
<|"Nest1" -> 1, "Nest2" -> 2, "Nest3" -> 2, "Nest4" -> 1, "a" -> 1,
"b" -> 4, "c" -> 7|>,
<|"Nest1" -> 1, "Nest2" -> 2, "Nest3" -> 2, "Nest4" -> 2, "a" -> 1,
"b" -> 4, "c" -> 7|>,
<|"Nest1" -> 2, "Nest2" -> 2, "Nest3" -> 2, "Nest4" -> 1, "a" -> 1,
"b" -> 4, "c" -> 7|>,
<|"Nest1" -> 2, "Nest2" -> 2, "Nest3" -> 2, "Nest4" -> 2, "a" -> 1,
"b" -> 4, "c" -> 7|>,
<|"Nest1" -> 2, "Nest2" -> 2, "Nest3" -> 3, "Nest4" -> 1, "a" -> 1,
"b" -> 4, "c" -> 7|>,
<|"Nest1" -> 2, "Nest2" -> 2, "Nest3" -> 3, "Nest4" -> 2, "a" -> 1,
"b" -> 4, "c" -> 7|>
}]
Further lets pretend:
- "Nest1" is some sort of condition (e.g. plant species)
- "Nest2" is some sort of variable (e.g. given sunlight or not)
- "Nest3" is some other sort of variable (e.g. amount watered)
- "Nest4" is replicates of some measurement
- "a","b","c" are some measurements (e.g. height, flowers, etc)
Which structure and why would be objectively better (in regards to leveraging the mechanics of the dataset object) for say comparing the average "height" ("a"
) across the two species ("Nest1") (without considering nests 2 and 3), as well as plotting the average number of flowers ("b") for each plant ("nest1") across each amount of water given ("nest3")?
Can example syntax be provided demonstrating the most native way to do this.
Mean[{a, b, c, d, e}] /. {a -> 10, b -> 20, c -> 30, d -> 40, e -> 50}
from a serialized (ie, flat) table, doesn't equal the iterated averagingMean[{Mean[{a, b}], Mean[{c, d, e}]}] /. {a -> 10, b -> 20, c -> 30, d -> 40, e -> 50}
that you'd get in the nested approach using something likeds[All,Mean,Mean,Mean,"a"]
. $\endgroup$