library("broadcast")
# recursive vector ====
x <- list(
setNames(1:11, letters[1:11]), 1:10, 1:9, 1:8, 1:7, 1:6, 1:5, 1:4, 1:3, 1:2, 1L, integer(0L)
)
names(x) <- month.abb
print(x)
## $Jan
## a b c d e f g h i j k
## 1 2 3 4 5 6 7 8 9 10 11
##
## $Feb
## [1] 1 2 3 4 5 6 7 8 9 10
##
## $Mar
## [1] 1 2 3 4 5 6 7 8 9
##
## $Apr
## [1] 1 2 3 4 5 6 7 8
##
## $May
## [1] 1 2 3 4 5 6 7
##
## $Jun
## [1] 1 2 3 4 5 6
##
## $Jul
## [1] 1 2 3 4 5
##
## $Aug
## [1] 1 2 3 4
##
## $Sep
## [1] 1 2 3
##
## $Oct
## [1] 1 2
##
## $Nov
## [1] 1
##
## $Dec
## integer(0)
cast_shallow2atomic(x, 0L)
## Jan.a Jan.b Jan.c Jan.d Jan.e Jan.f Jan.g Jan.h Jan.i Jan.j Jan.k Feb1 Feb2
## 1 2 3 4 5 6 7 8 9 10 11 1 2
## Feb3 Feb4 Feb5 Feb6 Feb7 Feb8 Feb9 Feb10 Mar1 Mar2 Mar3 Mar4 Mar5
## 3 4 5 6 7 8 9 10 1 2 3 4 5
## Mar6 Mar7 Mar8 Mar9 Apr1 Apr2 Apr3 Apr4 Apr5 Apr6 Apr7 Apr8 May1
## 6 7 8 9 1 2 3 4 5 6 7 8 1
## May2 May3 May4 May5 May6 May7 Jun1 Jun2 Jun3 Jun4 Jun5 Jun6 Jul1
## 2 3 4 5 6 7 1 2 3 4 5 6 1
## Jul2 Jul3 Jul4 Jul5 Aug1 Aug2 Aug3 Aug4 Sep1 Sep2 Sep3 Oct1 Oct2
## 2 3 4 5 1 2 3 4 1 2 3 1 2
## Nov
## 1
cast_shallow2atomic(x, 1L, comnames_from = 1L)
## Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
## a 1 1 1 1 1 1 1 1 1 1 1 NA
## b 2 2 2 2 2 2 2 2 2 2 NA NA
## c 3 3 3 3 3 3 3 3 3 NA NA NA
## d 4 4 4 4 4 4 4 4 NA NA NA NA
## e 5 5 5 5 5 5 5 NA NA NA NA NA
## f 6 6 6 6 6 6 NA NA NA NA NA NA
## g 7 7 7 7 7 NA NA NA NA NA NA NA
## h 8 8 8 8 NA NA NA NA NA NA NA NA
## i 9 9 9 NA NA NA NA NA NA NA NA NA
## j 10 10 NA NA NA NA NA NA NA NA NA NA
## k 11 NA NA NA NA NA NA NA NA NA NA NA
cast_shallow2atomic(x, -1L, comnames_from = 1L)
## a b c d e f g h i j k
## Jan 1 2 3 4 5 6 7 8 9 10 11
## Feb 1 2 3 4 5 6 7 8 9 10 NA
## Mar 1 2 3 4 5 6 7 8 9 NA NA
## Apr 1 2 3 4 5 6 7 8 NA NA NA
## May 1 2 3 4 5 6 7 NA NA NA NA
## Jun 1 2 3 4 5 6 NA NA NA NA NA
## Jul 1 2 3 4 5 NA NA NA NA NA NA
## Aug 1 2 3 4 NA NA NA NA NA NA NA
## Sep 1 2 3 NA NA NA NA NA NA NA NA
## Oct 1 2 NA NA NA NA NA NA NA NA NA
## Nov 1 NA NA NA NA NA NA NA NA NA NA
## Dec NA NA NA NA NA NA NA NA NA NA NA
# recursive matrix ====
x <- list(
setNames(1:11, letters[1:11]), 1:10, 1:9, 1:8, 1:7, 1:6, 1:5, 1:4, 1:3, 1:2, 1L, integer(0L)
) |> rev()
dim(x) <- c(3, 4)
dimnames(x) <- list(month.abb[1:3], month.name[1:4])
print(x)
## January February March April
## Jan integer,0 integer,3 integer,6 integer,9
## Feb 1 integer,4 integer,7 integer,10
## Mar integer,2 integer,5 integer,8 integer,11
cast_shallow2atomic(x, 0L)
##
## 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 1 2 3 4 5 6 1 2 3 4 5
##
## 6 7 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7
## a b c d e f g h i j k
## 8 9 10 1 2 3 4 5 6 7 8 9 10 11
cast_shallow2atomic(x, 1L, comnames_from = length(x))
## , , January
##
## Jan Feb Mar
## a NA 1 1
## b NA NA 2
## c NA NA NA
## d NA NA NA
## e NA NA NA
## f NA NA NA
## g NA NA NA
## h NA NA NA
## i NA NA NA
## j NA NA NA
## k NA NA NA
##
## , , February
##
## Jan Feb Mar
## a 1 1 1
## b 2 2 2
## c 3 3 3
## d NA 4 4
## e NA NA 5
## f NA NA NA
## g NA NA NA
## h NA NA NA
## i NA NA NA
## j NA NA NA
## k NA NA NA
##
## , , March
##
## Jan Feb Mar
## a 1 1 1
## b 2 2 2
## c 3 3 3
## d 4 4 4
## e 5 5 5
## f 6 6 6
## g NA 7 7
## h NA NA 8
## i NA NA NA
## j NA NA NA
## k NA NA NA
##
## , , April
##
## Jan Feb Mar
## a 1 1 1
## b 2 2 2
## c 3 3 3
## d 4 4 4
## e 5 5 5
## f 6 6 6
## g 7 7 7
## h 8 8 8
## i 9 9 9
## j NA 10 10
## k NA NA 11
cast_shallow2atomic(x, -1L, comnames_from = length(x))
## , , a
##
## January February March April
## Jan NA 1 1 1
## Feb 1 1 1 1
## Mar 1 1 1 1
##
## , , b
##
## January February March April
## Jan NA 2 2 2
## Feb NA 2 2 2
## Mar 2 2 2 2
##
## , , c
##
## January February March April
## Jan NA 3 3 3
## Feb NA 3 3 3
## Mar NA 3 3 3
##
## , , d
##
## January February March April
## Jan NA NA 4 4
## Feb NA 4 4 4
## Mar NA 4 4 4
##
## , , e
##
## January February March April
## Jan NA NA 5 5
## Feb NA NA 5 5
## Mar NA 5 5 5
##
## , , f
##
## January February March April
## Jan NA NA 6 6
## Feb NA NA 6 6
## Mar NA NA 6 6
##
## , , g
##
## January February March April
## Jan NA NA NA 7
## Feb NA NA 7 7
## Mar NA NA 7 7
##
## , , h
##
## January February March April
## Jan NA NA NA 8
## Feb NA NA NA 8
## Mar NA NA 8 8
##
## , , i
##
## January February March April
## Jan NA NA NA 9
## Feb NA NA NA 9
## Mar NA NA NA 9
##
## , , j
##
## January February March April
## Jan NA NA NA NA
## Feb NA NA NA 10
## Mar NA NA NA 10
##
## , , k
##
## January February March April
## Jan NA NA NA NA
## Feb NA NA NA NA
## Mar NA NA NA 11cast_shallow2atomic
Cast Shallow List to Atomic Object
Description
cast_shallow2atomic() casts a shallow (i.e. non-nested) list to an atomic object.
Usage
cast_shallow2atomic(x, ...)
## Default S3 method:
cast_shallow2atomic(x, arrangement = 0L, padding = NA, comnames_from = 1L, ...)
Arguments
x
|
a shallow (i.e. non-nested) list. The attributes of the objects inside the list will be ignored, except for names.
|
…
|
further arguments passed to or from methods. |
arrangement
|
see the Details and Examples sections.
|
padding
|
an atomic scalar, and only relevant if arrangement is 1 or -1. This gives the padding value to use when padding is required. Padding is used to ensure every all slices of the same dimension in the output have equal number of elements (for example, all rows must have the same number of columns). |
comnames_from
|
an integer scalar or NULL, and only relevant if arrangement is 1 or -1. This gives which element of x to use for the communal names. If NULL, no communal names will be given. For example: If x is a 1d (or dimensionless) list, cast_shallow2atomic(x, 1) will produce an atomic matrix. The column names of the matrix will be names(x). The row names, however, will be taken from names(x[[comnames_from]]), provided that x[[comnames_from]] has the proper length. See also the Examples section.
|
Details
If arrangement = 0L,
cast_shallow2atomic() works like unlist(), except that cast_shallow2atomic() guarantees an atomic vector result.
If arrangement = 1L,
cast_shallow2atomic() will produce an atomic array, with the elements arranged such that the dimensions are c(max(lengths(x)), dim(x)).
If x has no dimensions, dim(x) is replaced with length(x), thus treating x as an 1d array.
This will therefore always produce an atomic array with at least 2 dimensions.
If arrangement = -1L,
cast_shallow2atomic() will produce an atomic array, with the elements arranged such that the dimensions are c(dim(x), max(lengths(x))).
If x has no dimensions, dim(x) is replaced with length(x), thus treating x as an 1d array.
This will therefore always produce an atomic array with at least 2 dimensions.
Value
If arrangement = 0L:
An atomic vector without dimensions.
If arrangement = 1L:
An atomic array with dimensions c(max(lengths(x)), dim(x)).
The dimnames, if possible to construct, will be c(names(x[[comnames_from]]), dimnames(x)), provided If x has no dimensions, dim(x) is replaced with length(x).
If arrangement = -1L:
An atomic array with dimensions c(dim(x), max(lengths(x))).
If x has no dimensions, dim(x) is replaced with length(x).
Back transformation
From the casted atomic object,
out <- cast_shallow2atomic(x, …),
one can get an approximation of the original shallow list back using just base ‘R’ functions.
This section describes how to do so.
arrangement = 0L
If arrangement = 0L, one can transform an atomic object out back to a shallow list using:
back <- as.list(out)
names(back) <- names(out)
arrangement = 1L
If arrangement = 1L, one can transform an atomic object out back to a shallow list using:
asplit(out, seq(2, ndim(out)))
arrangement = -1L
If arrangement = -1L, one can transform an atomic object out back to a shallow list using:
asplit(out, seq(1, ndim(out) - 1L))