46  Nested Groups

The examples in Chapter 46 require

library(groupedHyperframe)

The nested grouping structure \(g_1/.../g_m\)

In fact, the ‘grouped’ extension of the S3 class 'hyperframe' is inspired by the S3 class 'groupedData' defined in package nlme (Pinheiro, Bates, and R Core Team 2025, v3.1.168), which inherits from the class 'data.frame'.

Package groupedHyperframe (v0.3.2.20251225) allows interaction terms using colon operator : in the (nested) grouping structure, e.g., ~g1/g2a:g2b/g3a:g3b:g3c. This feature is made possible because the colon operator : has higher priority than the forward slash / in R formula (Listing 46.1). In the meanwhile, user should be aware that the tilde operator ~ has lower priority than the forward pipe |> in R formula (Listing 46.2).

Listing 46.1: Advanced: colon operator : has higher priority than forward slash /
quote(g1/g2a:g2b/g3a:g3b:g3c) |>
  as.list()
# [[1]]
# `/`
# 
# [[2]]
# g1/g2a:g2b
# 
# [[3]]
# g3a:g3b:g3c
Listing 46.2: Advanced: tilde operator ~ has lower priority than forward pipe |>
list(
  ~ x |> foobar(), # wrong!
  quote((~ x) |> foobar()) # correct
)
# [[1]]
# ~foobar(x)
# 
# [[2]]
# foobar((~x))

The low-level utility function get_nested() breaks down a nested grouping structure by the forward slash / (Listing 46.3 - Listing 46.6).

Listing 46.3: Example: function get_nested() on call
quote(g1/g2/g3) |>
  get_nested()
# $g1
# g1
# 
# $g2
# g2
# 
# $g3
# g3
Listing 46.4: Example: function get_nested() on formula
(~g1/g2/g3) |>
  get_nested()
# $g1
# g1
# 
# $g2
# g2
# 
# $g3
# g3
Listing 46.5: Example: function get_nested() on call, with interaction terms
quote(g1/g2a:g2b/g3a:g3b:g3c) |>
  get_nested()
# $g1
# g1
# 
# $`g2a:g2b`
# g2a:g2b
# 
# $`g3a:g3b:g3c`
# g3a:g3b:g3c
Listing 46.6: Example: function get_nested() on formula, with interaction terms
(~g1/g2a:g2b/g3a:g3b:g3c) |>
  get_nested()
# $g1
# g1
# 
# $`g2a:g2b`
# g2a:g2b
# 
# $`g3a:g3b:g3c`
# g3a:g3b:g3c

Listing 46.7 and Listing 46.8 the exception handling of function get_nested() when no nested group exists.

Listing 46.7: Exception: function get_nested() on formula, no nested group
(~a) |> get_nested()
# $a
# a
Listing 46.8: Exception: function get_nested() on formula with interaction term, but no nested group
(~b:c) |> get_nested()
# $`b:c`
# b:c

The (nested) grouping structure attr(,'group') of a grouped hyper data frame (Chapter 25)