Skip to contents

Convenience wrapper around base::rbind() for stacking a (named) list of data frames while recording the source list element in a label column. When the list is named, the names are used as labels; otherwise, integer indices ("1", "2", …) are used. Columns absent in some elements are filled with NA so inputs need only share column names in common.

Usage

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

Arguments

x

A list of data frames (or objects coercible to data frames).

label

A character scalar giving the identifier column name to add. Set to NULL to omit the identifier column. Default is "model".

label_type

Desired type for the identifier column: automatic conversion via utils::type.convert() ("auto", the default), or explicit coercion to "numeric", "character", or "factor".

Value

A single data frame produced by row-binding the list elements. If label is not NULL, the identifier column is the first column in the output.

Examples

lst <- list(
  retro_1 = data.frame(age = 2:4, rho = runif(3)),
  retro_2 = data.frame(age = 2:4, rho = runif(3))
)
stack_list(lst, label = "retro")
#>     retro age       rho
#> 1 retro_1   2 0.7176185
#> 2 retro_1   3 0.9919061
#> 3 retro_1   4 0.3800352
#> 4 retro_2   2 0.7774452
#> 5 retro_2   3 0.9347052
#> 6 retro_2   4 0.2121425