relist_named | R Documentation |
relist a vector which imposes the model object list structure while allowing vector elements and names to be re-ordered
relist_named(x, skeleton, ...)
x |
vector to be applied to the |
skeleton |
|
... |
additional arguments are ignored. |
This function is a simple update to utils::relist()
that allows the order of vectors to change, alongside the
correct names for each element.
More specifically, this function does not replace the
updated names with the corresponding names from
the list skeleton
, as is the case in default implementation of
utils::relist()
.
This function is called by mixedSorts()
which iteratively calls
mixedOrder()
on each vector component of the input list
,
and permits nested lists. The result is a single sorted vector
which is split into the list
components, then relist-ed to
the original structure. During the process, it is important
to retain vector names in the order defined by mixedOrder()
.
list
object with the same structure as the skeleton
.
Other jam list functions:
cPasteSU()
,
cPasteS()
,
cPasteUnique()
,
cPasteU()
,
cPaste()
,
heads()
,
jam_rapply()
,
list2df()
,
mergeAllXY()
,
mixedSorts()
,
rbindList()
,
rlengths()
,
sclass()
,
sdim()
,
uniques()
,
unnestList()
# generate nested list
x <- list(A=nameVector(LETTERS[3:1]),
B=list(
E=nameVector(LETTERS[10:7]),
D=nameVector(LETTERS[5:4])),
C=list(
G=nameVector(LETTERS[19:16]),
F=nameVector(LETTERS[15:11]),
H=list(
I=nameVector(LETTERS[22:20]))
))
x
# unlisted vector of items
xu <- unlist(unname(x))
# unlisted vector of names
xun <- unname(jam_rapply(x, names));
names(xu) <- xun;
# recursive list element lengths
xrn <- jam_rapply(x, length);
# define factor in order of list structure
xn <- factor(
rep(names(xrn),
xrn),
levels=names(xrn));
# re-create the original list
xu_new <- unlist(unname(split(xu, xn)))
xnew <- relist_named(xu_new, x);
xnew
# re-order elements
k <- mixedOrder(xu_new);
xuk <- unlist(unname(split(xu[k], xn[k])))
xk <- relist_named(xuk, x);
xk
# the default relist() function does not support this use case
xdefault <- relist(xuk, x);
xdefault
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.