library(groupedHyperframe)46 Nested Groups
The examples in Chapter 46 require
The nested grouping structure \(g_1/.../g_m\)
- is denoted by R
formula~g1/.../gm - is explained in section Details of
stats::formuladocumentation; - is made well known by the parameter
randomof functionsnlme::lme()andnlme::nlme().
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).
: has higher priority than forward slash /
quote(g1/g2a:g2b/g3a:g3b:g3c) |>
as.list()
# [[1]]
# `/`
#
# [[2]]
# g1/g2a:g2b
#
# [[3]]
# g3a:g3b:g3c~ 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).
get_nested() on call
quote(g1/g2/g3) |>
get_nested()
# $g1
# g1
#
# $g2
# g2
#
# $g3
# g3get_nested() on formula
(~g1/g2/g3) |>
get_nested()
# $g1
# g1
#
# $g2
# g2
#
# $g3
# g3get_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:g3cget_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:g3cListing 46.7 and Listing 46.8 the exception handling of function get_nested() when no nested group exists.
get_nested() on formula, no nested group
(~a) |> get_nested()
# $a
# aget_nested() on formula with interaction term, but no nested group
(~b:c) |> get_nested()
# $`b:c`
# b:cThe (nested) grouping structure attr(,'group') of a grouped hyper data frame (Chapter 25)
- must denote the highest level using a
symbol(e.g.,g1) - may denote the lower levels using a
symbol(e.g.,g2) or an interaction-term (e.g.,g2a:g2b)