unlist(data, recursive=T, use.names=T)
names attribute for
the resulting
object, using the component names (possibly at several levels) of
data?
If
FALSE, the resulting object will not have a names attribute.
If
TRUE, the function will use the rule below to construct a
names
attribute for the result.
c) the components of
data.
If
recursive is
TRUE, each component of
data is
unlisted
(with
recursive=TRUE) before it is combined.
Thinking of
data as a tree, the effect of
unlist with
recursive=T
is to produce a vector of all the leaves.
The mode of the result is the most general of all the modes of the
components of
data, or of the leaves of
data if
recursive is
TRUE.
If
use.names is
TRUE, names are built from the component
names according to the following rules:
Suppose
x is a component of
data and
xname represents its corresponding
name;
then, in the unlisted version,
x has names which are made by pasting
xname
and the
names attribute of
x, with separator
"."
(assuming that both strings are non-empty).
If
xname is the empty string (i.e.,
x has no component name)
just the
names attribute of
x is used, if any.
If
xname is non-empty but
x has no
names attribute, then
xname is used
followed by the index of the corresponding element in
x (no separating
"."
in this case).
If
x has length 1, the redundant
"1" is not pasted on.
If
recursive is
TRUE, the rule above is applied, bottom up, recursively.
The same rule applies to names created by the
c() function, with the
equivalence shown below.
The algorithm does not enforce rules on the names, such
as uniqueness or that every name be non-empty.
Use
names(x) <- make.names(names(x), unique=T)
to enforce such rules.
c(..., recursive=T) is equivalent to
unlist(list(...), recursive=T).
lista <- list(1:3, a=5:7, b=list(b1=12:14, b2=c(23, 25, 28)))
unlist(lista) # a vector of length 12
unlist(lista, recursive=F)
# a list of length 8 with final components b.b1 and b.b2
# equivalent to list(1, 2, 3, a1=5, a2=6, a3=7, b.b1=12:14, b.b2=c(23,25,28))
# build a vector of variable-length results where 'fun' returns a list
x <- vector("list", n)
for(i in 1:n)
x[[i]] <- fun(i)
x <- unlist(x, recursive=F)
x <- list(A=1,B=c(a=2,b=3,4),list(d=5,6)) # names construction
unlist(x)
A B.a B.b B3 d
1 2 3 4 5 6