Skip to contents

Consider the situation where a function returns a list containing two data.frames. If this function is called repeatedly and the return values are stored in a list, we will have a list of fully named lists (each of which contains a data.frame). Typically, we want to extract the two data.frames from this nested list structure (and rbindlist them).

Usage

unnest_dfs_within_list_of_fully_named_lists(
  x,
  returned_name_when_dfs_are_not_nested = "data",
  ...
)

Arguments

x

A list of fully named lists (which then contain data.frames)

returned_name_when_dfs_are_not_nested

When x is a single list of data.frames, what name should be returned?

...

parameters passed to data.table::rbindlist

Value

A fully named list, each element a data.table. NULL if x is not a list.

Details

When every element of x is NULL or a data.frame, those elements are row-bound into a single data.table, which is returned in a length-1 list named by returned_name_when_dfs_are_not_nested.

Otherwise every element of x must be NULL or a fully named list, and the function stops with an error if one of them is not. The returned list is named by the sorted union of the inner names, and each element is the data.table::rbindlist() of the inner elements that carry that name.

See also

vignette("csutil", package = "csutil"), which unnests a two-element list of fully named lists.

Examples

x <- list(
  list(
    "a" = data.frame("v1"=1),
    "b" = data.frame("v2"=3)
  ),
  list(
    "a" = data.frame("v1"=10),
    "b" = data.frame("v2"=30),
    "d" = data.frame("v3"=50)
  ),
  list(
    "a" = NULL
  ),
  NULL
)
print(x)
#> [[1]]
#> [[1]]$a
#>   v1
#> 1  1
#> 
#> [[1]]$b
#>   v2
#> 1  3
#> 
#> 
#> [[2]]
#> [[2]]$a
#>   v1
#> 1 10
#> 
#> [[2]]$b
#>   v2
#> 1 30
#> 
#> [[2]]$d
#>   v3
#> 1 50
#> 
#> 
#> [[3]]
#> [[3]]$a
#> NULL
#> 
#> 
#> [[4]]
#> NULL
#> 
csutil::unnest_dfs_within_list_of_fully_named_lists(x)
#> $a
#>       v1
#>    <num>
#> 1:     1
#> 2:    10
#> 
#> $b
#>       v2
#>    <num>
#> 1:     3
#> 2:    30
#> 
#> $d
#>       v3
#>    <num>
#> 1:    50
#> 

x <- list(
  data.frame("v1"=1),
  data.frame("v3"=50)
)
print(x)
#> [[1]]
#>   v1
#> 1  1
#> 
#> [[2]]
#>   v3
#> 1 50
#> 
csutil::unnest_dfs_within_list_of_fully_named_lists(
  x,
  returned_name_when_dfs_are_not_nested = "NAME",
  fill = TRUE
)
#> $NAME
#>       v1    v3
#>    <num> <num>
#> 1:     1    NA
#> 2:    NA    50
#>