library("broadcast")
# 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) # 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
# in2out demonstration ====
x <- list(
group1 = list(
class1 = list(
height = rnorm(10, 170),
weight = rnorm(10, 80),
sex = sample(c("M", "F", NA), 10, TRUE)
),
class2 = list(
height = rnorm(10, 170),
weight = rnorm(10, 80),
sex = sample(c("M", "F", NA), 10, TRUE)
)
),
group2 = list(
class1 = list(
height = rnorm(10, 170),
weight = rnorm(10, 80),
sex = sample(c("M", "F", NA), 10, TRUE)
),
class2 = list(
height = rnorm(10, 170),
weight = rnorm(10, 80),
sex = sample(c("M", "F", NA), 10, TRUE)
)
)
)
# in2out = TRUE (default):
x2 <- cast_hier2dim(x)
dimnames(x2) <- hiernames2dimnames(x)
print(x2)
#> , , group1
#>
#> class1 class2
#> height numeric,10 numeric,10
#> weight numeric,10 numeric,10
#> sex character,10 character,10
#>
#> , , group2
#>
#> class1 class2
#> height numeric,10 numeric,10
#> weight numeric,10 numeric,10
#> sex character,10 character,10
cast_dim2flat(x2[1,1,,drop = FALSE])
#> $`['height', 'class1', 'group1']`
#> [1] 170.7599 170.8500 171.5168 170.1504 168.9218 169.6336 171.4334 170.5334
#> [9] 170.3046 169.6787
#>
#> $`['height', 'class1', 'group2']`
#> [1] 171.5328 169.2387 170.6076 168.8564 170.5531 169.7537 169.0308 168.8972
#> [9] 169.2301 171.5122
# in2out = FALSE:
x2 <- cast_hier2dim(x, in2out = FALSE)
dimnames(x2) <- hiernames2dimnames(x, in2out = FALSE)
print(x2)
#> , , height
#>
#> class1 class2
#> group1 numeric,10 numeric,10
#> group2 numeric,10 numeric,10
#>
#> , , weight
#>
#> class1 class2
#> group1 numeric,10 numeric,10
#> group2 numeric,10 numeric,10
#>
#> , , sex
#>
#> class1 class2
#> group1 character,10 character,10
#> group2 character,10 character,10
cast_dim2flat(x2[1,1,,drop = FALSE])
#> $`['group1', 'class1', 'height']`
#> [1] 170.7599 170.8500 171.5168 170.1504 168.9218 169.6336 171.4334 170.5334
#> [9] 170.3046 169.6787
#>
#> $`['group1', 'class1', 'weight']`
#> [1] 80.16666 80.53628 80.29969 79.40805 80.99850 79.56870 79.66241 81.47025
#> [9] 77.23473 79.24235
#>
#> $`['group1', 'class1', 'sex']`
#> [1] "M" "M" "M" "F" "F" "M" NA NA "F" "M"Details on Casting Functions
Description
‘broadcast’ provides several "casting" functions.
These can facilitate complex forms of broadcasting that would normally not be possible.
But these "casting" functions also have their own merit, beside empowering complex broadcasting.
The following casting functions are available:
-
acast:
Casts group-based subsets of an array into a new dimension.
Useful for, for example, performing grouped broadcasted operations. -
cast_hier2dim:
Casts a nested/hierarchical list into a dimensional list (i.e. array of typelist).
Useful because one cannot broadcast through nesting, but one can broadcast along dimensions. -
hier2dim, hiernames2dimnames:
Helper functions for cast_hier2dim. -
cast_dim2hier:
Casts a dimensional list into a nested/hierarchical list; the opposite of cast_hier2dim. -
cast_shallow2atomic:
Casts a (dimensional) shallow (i.e. non-nested) list into an atomic vector or array.
Useful because atomic vectors/arrays have access to many vectorized (broadcasted) operations that may not be available for vectors/arrays of typelist. -
cast_dim2flat:
Casts a dimensional list into a flattened list, but with names that indicate their original dimensional positions.
Mostly useful for printing or summarizing dimensional lists. -
dropnests:
Drop redundant nesting in lists; mostly used for facilitating the above casting functions.