consolidate | R Documentation |
Consolidates a list of lists into a regular list by combining like-named elements.
consolidate(x)
## S3 method for class 'data.frame'
consolidate(x)
## S3 method for class 'exdf'
consolidate(x)
x |
A list of lists |
consolidate
is generic, with methods defined for nested
lists of data frames and exdf
objects.
A list with elements named name_1
, name_2
, ..., name_M
,
where each element was created by combining all elements of x
with the
same name using rbind
; for example, the element with name name_1
will be created by calling rbind(list_1$name_1, list_2$name_1, ...,
list_N$name_1)
. Before calling rbind
, each element will be limited to
the columns that are common to all elements with the same name.
exdf
# Example 1: Create a nested list of data frames and then consolidate them into
# a regular list by combining the like-named elements
nested_df_list <- list(
list_1 = list(
name_1 = data.frame(A = c(1, 2), B = c(0, 0)),
name_2 = data.frame(A = c(3, 4), B = c(0, 0)),
name_3 = data.frame(A = c(5, 6), B = c(0, 0))
),
list_2 = list(
name_1 = data.frame(A = c(7, 8), B = c(0, 0)),
name_2 = data.frame(A = c(9, 10), B = c(0, 0)),
name_3 = data.frame(A = c(11, 12), B = c(0, 0))
),
list_3 = list(
name_1 = data.frame(A = c(13, 14), B = c(0, 0)),
name_2 = data.frame(A = c(15, 16), B = c(0, 0)),
name_3 = data.frame(A = c(17, 18), B = c(0, 0))
)
)
str(nested_df_list)
consolidated_df_list <- consolidate(nested_df_list)
str(consolidated_df_list)
# Example 2: Create a nested list of `exdf` objects and then consolidate them
# into a regular list by combining the like-named elements. Here, some of the
# elements have columns not present in the others (for example,
# `nested_exdf_list$list_3$name_1`). However, these "extra" columns are removed
# before calling `rbind` and they do not appear in `consolidated_exdf_list`.
nested_exdf_list <- list(
list_1 = list(
name_1 = exdf(data.frame(A = c(1, 2), B = c(0, 0))),
name_2 = exdf(data.frame(A = c(3, 4), B = c(0, 0))),
name_3 = exdf(data.frame(A = c(5, 6), B = c(0, 0)))
),
list_2 = list(
name_1 = exdf(data.frame(A = c(7, 8), B = c(0, 0))),
name_2 = exdf(data.frame(A = c(9, 10), B = c(0, 0))),
name_3 = exdf(data.frame(A = c(11, 12), B = c(0, 0)))
),
list_3 = list(
name_1 = exdf(data.frame(A = c(13, 14), B = c(0, 0), C = c(-1, -2))),
name_2 = exdf(data.frame(A = c(15, 16), B = c(0, 0), C = c(-1, -2))),
name_3 = exdf(data.frame(A = c(17, 18), B = c(0, 0), C = c(-1, -2)))
)
)
str(nested_exdf_list)
consolidated_exdf_list <- consolidate(nested_exdf_list)
str(consolidated_exdf_list)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.