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

## Default S3 method:
dropnests(x, maxdepth = 16L, recurse_all = 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_all see broadcast_casting.

Value

A list without redundant nesting.
Attributes are preserved.

See Also

broadcast_casting

Examples

library("broadcast")


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

print(x)
#> $a
#> $a[[1]]
#> $a[[1]][[1]]
#> $a[[1]][[1]][[1]]
#> $a[[1]][[1]][[1]][[1]]
#>  [1]  1  2  3  4  5  6  7  8  9 10
#> 
#> 
#> 
#> 
#> 
#> $b
#> $b[[1]]
#>  [1]  1  2  3  4  5  6  7  8  9 10

dropnests(x)
#> $a
#>  [1]  1  2  3  4  5  6  7  8  9 10
#> 
#> $b
#>  [1]  1  2  3  4  5  6  7  8  9 10


# 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
#> $a
#>  [1]  1  2  3  4  5  6  7  8  9 10
#> 
#> $b
#>    month.abb month.name
#> 1        Jan    January
#> 2        Feb   February
#> 3        Mar      March
#> 4        Apr      April
#> 5        May        May
#> 6        Jun       June
#> 7        Jul       July
#> 8        Aug     August
#> 9        Sep  September
#> 10       Oct    October
#> 11       Nov   November
#> 12       Dec   December
#> 
#> $c
#>    month.abb
#> 1        Jan
#> 2        Feb
#> 3        Mar
#> 4        Apr
#> 5        May
#> 6        Jun
#> 7        Jul
#> 8        Aug
#> 9        Sep
#> 10       Oct
#> 11       Nov
#> 12       Dec
#> 
#> $d
#> , , 1
#> 
#>      [,1]
#> [1,] 1

dropnests(x, recurse_all = TRUE)
#> $a
#>  [1]  1  2  3  4  5  6  7  8  9 10
#> 
#> $b
#>    month.abb month.name
#> 1        Jan    January
#> 2        Feb   February
#> 3        Mar      March
#> 4        Apr      April
#> 5        May        May
#> 6        Jun       June
#> 7        Jul       July
#> 8        Aug     August
#> 9        Sep  September
#> 10       Oct    October
#> 11       Nov   November
#> 12       Dec   December
#> 
#> $c
#>  [1] "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"
#> 
#> $d
#> [1] 1


# maxdepth demonstration ====
x <- list(
  a = list(list(list(list(1:10)))),
  b = list(1:10)
)
print(x)
#> $a
#> $a[[1]]
#> $a[[1]][[1]]
#> $a[[1]][[1]][[1]]
#> $a[[1]][[1]][[1]][[1]]
#>  [1]  1  2  3  4  5  6  7  8  9 10
#> 
#> 
#> 
#> 
#> 
#> $b
#> $b[[1]]
#>  [1]  1  2  3  4  5  6  7  8  9 10

dropnests(x) # by default, maxdepth = 16
#> $a
#>  [1]  1  2  3  4  5  6  7  8  9 10
#> 
#> $b
#>  [1]  1  2  3  4  5  6  7  8  9 10

dropnests(x, maxdepth = 3L)
#> $a
#> $a[[1]]
#> $a[[1]][[1]]
#>  [1]  1  2  3  4  5  6  7  8  9 10
#> 
#> 
#> 
#> $b
#>  [1]  1  2  3  4  5  6  7  8  9 10

dropnests(x, maxdepth = 1L) # returns `x` unchanged
#> $a
#> $a[[1]]
#> $a[[1]][[1]]
#> $a[[1]][[1]][[1]]
#> $a[[1]][[1]][[1]][[1]]
#>  [1]  1  2  3  4  5  6  7  8  9 10
#> 
#> 
#> 
#> 
#> 
#> $b
#> $b[[1]]
#>  [1]  1  2  3  4  5  6  7  8  9 10