relist() is an S3 generic function with a few methods in order
to allow easy inversion of
unlist(obj) when that is used
with an object
obj of (S3) class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
relist(flesh, skeleton) ## Default S3 method: relist(flesh, skeleton = attr(flesh, "skeleton")) ## S3 method for class 'factor' relist(flesh, skeleton = attr(flesh, "skeleton")) ## S3 method for class 'list' relist(flesh, skeleton = attr(flesh, "skeleton")) ## S3 method for class 'matrix' relist(flesh, skeleton = attr(flesh, "skeleton")) as.relistable(x) is.relistable(x) ## S3 method for class 'relistable' unlist(x, recursive = TRUE, use.names = TRUE)
a vector to be relisted
a list, the structure of which determines the structure of the result
an R object, typically a list (or vector).
logical. Should unlisting be applied to list
logical. Should names be preserved?
Some functions need many parameters, which are most easily represented in
complex structures, e.g., nested lists. Unfortunately, many
mathematical functions in R, including
nlm can only operate on functions whose domain is
a vector. R has
unlist() to convert nested list
objects into a vector representation.
relist(), its methods and
the functionality mentioned here provide the inverse operation to convert
vectors back to the convenient structural representation.
This allows structured functions (such as
optim()) to have simple
For example, a likelihood function for a multivariate normal model needs a variance-covariance matrix and a mean vector. It would be most convenient to represent it as a list containing a vector and a matrix. A typical parameter might look like
optim cannot operate on functions that take
lists as input; it only likes numeric vectors. The solution is
conversion. Given a function
mvdnorm(x, mean, vcov, log = FALSE)
which computes the required probability density, then
1 2 3 4 5 6 7 8 9 10 11
relist takes two parameters: skeleton and flesh. Skeleton is a sample
object that has the right
shape but the wrong content.
is a vector with the right content but the wrong shape. Invoking
will put the content of flesh on the skeleton. You don't need to specify
skeleton explicitly if the skeleton is stored as an attribute inside flesh.
In particular, if flesh was created from some object obj with
then the skeleton attribute is automatically set. (Note that this
does not apply to the example here, as
optim is creating
a new vector to pass to
ll and not its
As long as
skeleton has the right shape, it should be a precise inverse
unlist. These equalities hold:
1 2 3 4 5
an object of (S3) class
R Core, based on a code proposal by Andrew Clausen.
1 2 3 4 5
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.