dropnests: Drop Redundant List Nesting

View source: R/dropnests.R

dropnestsR Documentation

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, ...)

## Default S3 method:
dropnests(x, maxdepth = 16L, recurse_classed = FALSE, ...)

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_classed

TRUE or FALSE, indicating if the function should also recurse through classed lists within x, like data.frames.

Value

A list without redundant nesting.
Attributes are preserved.

See Also

broadcast_casting

Examples


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


dropnests(x)


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


dropnests(x) # by default, recurse_classed = FALSE

dropnests(x, recurse_classed = 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


broadcast documentation built on Sept. 15, 2025, 5:08 p.m.