View source: R/cast_shallow2atomic.R
| cast_shallow2atomic | R Documentation |
cast_shallow2atomic()
casts a shallow (i.e. non-nested) list to an atomic vector/array.
cast_shallow2atomic(x, ...)
## Default S3 method:
cast_shallow2atomic(x, arrangement = 0L, padding = NA, comnames_from = 1L, ...)
x |
a shallow (i.e. non-nested) list. |
... |
further arguments passed to or from methods. |
arrangement |
see the |
padding |
an atomic scalar, and only relevant if |
comnames_from |
an integer scalar or |
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.
The dimnames, if possible to construct,
will be c(dimnames(x[[comnames_from]]), dimnames(x)).
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.
The dimnames, if possible to construct,
will be c(dimnames(x), names(x[[comnames_from]])).
In all cases, the result will be atomic.
If arrangement = 0L:
An atomic vector.
If arrangement = 1L:
An atomic array.
If arrangement = -1L:
An atomic array.
The type of the result is determined from the highest atomic type of any of the list elements
(including elements of length zero).
The hierarchy of atomic types is:
raw < logical < integer < double < complex < character.
List elements that are not atomic but language expressions,
like formulas,
will be coerced to type of character.
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.
If arrangement = 0L,
one can transform an atomic object out back to a shallow list using:
back <- as.list(out)
names(back) <- names(out)
If arrangement = 1L,
one can transform an atomic object out back to a shallow list using:
asplit(out, seq(2, ndim(out)))
If arrangement = -1L,
one can transform an atomic object out back to a shallow list using:
asplit(out, seq(1, ndim(out) - 1L))
broadcast_casting
# 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)
cast_shallow2atomic(x, 0L)
cast_shallow2atomic(x, 1L, comnames_from = 1L)
cast_shallow2atomic(x, -1L, comnames_from = 1L)
# 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)
cast_shallow2atomic(x, 0L)
cast_shallow2atomic(x, 1L, comnames_from = length(x))
cast_shallow2atomic(x, -1L, comnames_from = length(x))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.