cast_dim2hier

Cast Dimensional List into Hierarchical List

Description

cast_dim2hier() casts a dimensional list (i.e. an array of type list) into a hierarchical/nested list.

Usage

cast_dim2hier(x, ...)

## Default S3 method:
cast_dim2hier(x, in2out = TRUE, distr.names = TRUE, ...)

Arguments

x an array of type list.
… further arguments passed to or from methods.

in2out see broadcast_casting.
distr.names TRUE or FALSE, indicating if dimnames from x should be distributed over the nested elements of the output.
See examples section for demonstration.

Value

A nested list.

See Also

broadcast_casting

Examples

library("broadcast")



x <- array(c(as.list(1:24), as.list(letters)), 4:2)
dimnames(x) <- list(
  letters[1:4],
  LETTERS[1:3],
  month.abb[1:2]
)
print(x)
#> , , Jan
#> 
#>   A B C 
#> a 1 5 9 
#> b 2 6 10
#> c 3 7 11
#> d 4 8 12
#> 
#> , , Feb
#> 
#>   A  B  C 
#> a 13 17 21
#> b 14 18 22
#> c 15 19 23
#> d 16 20 24


# cast `x` from in to out, and distribute names:
x2 <- cast_dim2hier(x, distr.names = TRUE)
head(x2, n = 2)
#> $Jan
#> $Jan$A
#> $Jan$A$a
#> [1] 1
#> 
#> $Jan$A$b
#> [1] 2
#> 
#> $Jan$A$c
#> [1] 3
#> 
#> $Jan$A$d
#> [1] 4
#> 
#> 
#> $Jan$B
#> $Jan$B$a
#> [1] 5
#> 
#> $Jan$B$b
#> [1] 6
#> 
#> $Jan$B$c
#> [1] 7
#> 
#> $Jan$B$d
#> [1] 8
#> 
#> 
#> $Jan$C
#> $Jan$C$a
#> [1] 9
#> 
#> $Jan$C$b
#> [1] 10
#> 
#> $Jan$C$c
#> [1] 11
#> 
#> $Jan$C$d
#> [1] 12
#> 
#> 
#> 
#> $Feb
#> $Feb$A
#> $Feb$A$a
#> [1] 13
#> 
#> $Feb$A$b
#> [1] 14
#> 
#> $Feb$A$c
#> [1] 15
#> 
#> $Feb$A$d
#> [1] 16
#> 
#> 
#> $Feb$B
#> $Feb$B$a
#> [1] 17
#> 
#> $Feb$B$b
#> [1] 18
#> 
#> $Feb$B$c
#> [1] 19
#> 
#> $Feb$B$d
#> [1] 20
#> 
#> 
#> $Feb$C
#> $Feb$C$a
#> [1] 21
#> 
#> $Feb$C$b
#> [1] 22
#> 
#> $Feb$C$c
#> [1] 23
#> 
#> $Feb$C$d
#> [1] 24

# cast `x` from out to in, and distribute names:
x2 <- cast_dim2hier(x, in2out = FALSE, distr.names = TRUE)
head(x2, n = 2)
#> $a
#> $a$A
#> $a$A$Jan
#> [1] 1
#> 
#> $a$A$Feb
#> [1] 13
#> 
#> 
#> $a$B
#> $a$B$Jan
#> [1] 5
#> 
#> $a$B$Feb
#> [1] 17
#> 
#> 
#> $a$C
#> $a$C$Jan
#> [1] 9
#> 
#> $a$C$Feb
#> [1] 21
#> 
#> 
#> 
#> $b
#> $b$A
#> $b$A$Jan
#> [1] 2
#> 
#> $b$A$Feb
#> [1] 14
#> 
#> 
#> $b$B
#> $b$B$Jan
#> [1] 6
#> 
#> $b$B$Feb
#> [1] 18
#> 
#> 
#> $b$C
#> $b$C$Jan
#> [1] 10
#> 
#> $b$C$Feb
#> [1] 22