relist_named: relist a vector which allows re-ordered names

relist_namedR Documentation

relist a vector which allows re-ordered names

Description

relist a vector which imposes the model object list structure while allowing vector elements and names to be re-ordered

Usage

relist_named(x, skeleton, ...)

Arguments

x

vector to be applied to the skeleton list structure in order.

skeleton

list object representing the desired final list structure, or vector when the input data x should be returned as-is, without change. Specifically, when skeleton is a vector, the names(x) are maintained without change.

...

additional arguments are ignored.

Details

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().

Value

list object with the same structure as the skeleton.

See Also

Other jam list functions: cPasteSU(), cPasteS(), cPasteUnique(), cPasteU(), cPaste(), heads(), jam_rapply(), list2df(), mergeAllXY(), mixedSorts(), rbindList(), rlengths(), sclass(), sdim(), uniques(), unnestList()

Examples

# 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


jmw86069/jamba documentation built on Oct. 9, 2024, 10:52 a.m.