Skip to contents

Stack identically named subtables from a nested list

Usage

stack_nested(
  x,
  label = "model",
  label_type = c("auto", "numeric", "character", "factor")
)

Arguments

x

A named list of results (e.g. sims or models), each containing a named list of data.frames (e.g. "ssb", "N", "recruitment", ...). Shape: list( = list( = data.frame, ...), ...)

label

Name of the column to add with the outer id. Default "model". Set to NULL to omit the id column.

label_type

Controls how the label column is coerced. One of:

  • "auto" (default): numeric when possible, else character.

  • "numeric": force numeric conversion (with NA for non-numeric).

  • "character": keep as character.

  • "factor": convert to factor.

Value

A named list of data.frames. One element per subtable name. Each data.frame is the row-bound stack across outer ids, with an added column named by label (unless label = NULL).

Examples

res <- list(
  sim1 = list(
    ssb = data.frame(year = 1:3, est = 1:3),
    N = data.frame(year = 1:2, age = 2:3, est = 5:6)
  ),
  sim2 = list(
    ssb = data.frame(year = 1:3, est = 11:13),
    N = data.frame(year = 1:2, age = 2:3, est = 15:16)
  )
)
stacked <- stack_nested(res, label = "sim")
str(stacked$ssb) # has column 'sim'
#> 'data.frame':	6 obs. of  3 variables:
#>  $ sim : chr  "sim1" "sim1" "sim1" "sim2" ...
#>  $ year: int  1 2 3 1 2 3
#>  $ est : int  1 2 3 11 12 13