Learn R Programming

broadcast (version 0.1.7)

dropnests: Drop Redundant List Nesting

Description

dropnests() drops redundant nesting of a list.
It is the hierarchical equivalent to the dimensional base::drop() function.

Usage

dropnests(x, ...)

# S3 method for default dropnests(x, maxdepth = 16L, recurse_all = FALSE, ...)

Value

A list without redundant nesting.

Attributes are preserved.


Arguments

x

a list

...

further arguments passed to or from methods.

maxdepth

a single, positive integer, giving the maximum depth to recurse into the list.
The surface-level elements of a list is depth 1.
dropnests(x, maxdepth = 1) will return x unchanged.

recurse_all

see broadcast_casting.

See Also

broadcast_casting

Examples

Run this code

x <- list(
  a = list(list(list(list(1:10)))),
  b = list(1:10)
)

print(x)

dropnests(x)


# recurse_all demonstration ====
x <- list(
  a = list(list(list(list(1:10)))),
  b = data.frame(month.abb, month.name),
  c = data.frame(month.abb),
  d = array(list(1), c(1,1,1))
)

dropnests(x) # by default, recurse_all = FALSE

dropnests(x, recurse_all = TRUE)


# maxdepth demonstration ====
x <- list(
  a = list(list(list(list(1:10)))),
  b = list(1:10)
)
print(x)

dropnests(x) # by default, maxdepth = 16

dropnests(x, maxdepth = 3L)

dropnests(x, maxdepth = 1L) # returns `x` unchanged

Run the code above in your browser using DataLab