Lists -- Generic and Dotted Pairs
Functions to construct, coerce and check for both kinds of R lists.
list(...) pairlist(...)as.list(x, ...) "as.list"(x, all.names = FALSE, sorted = FALSE, ...) as.pairlist(x)is.list(x) is.pairlist(x)alist(...)
- objects, possibly named.
- object to be coerced or tested.
- a logical indicating whether to copy all values or (default) only those whose names do not begin with a dot.
- a logical indicating whether the
namesof the resulting list should be sorted (increasingly). Note that this is somewhat costly, but may be useful for comparison of environments.
Almost all lists in R internally are Generic Vectors, whereas
traditional dotted pair lists (as in LISP) remain available but
rarely seen by users (except as
formals of functions).
The arguments to
pairlist are of the form
tag = value. The functions return a list or
dotted pair list composed of its arguments with each value either
tagged or untagged, depending on how the argument was specified.
alist handles its arguments as if they described function
arguments. So the values are not evaluated, and tagged arguments with
no value are allowed whereas
list simply ignores them.
alist is most often used in conjunction with
as.list attempts to coerce its argument to a list. For
functions, this returns the concatenation of the list of formal
arguments and the function body. For expressions, the list of
constituent elements is returned.
as.list is generic, and as
the default method calls
as.vector(mode = "list") for a
non-list, methods for
as.vector may be invoked.
turns a factor into a list of one-element factors. Attributes may
be dropped unless the argument already is a list or expression. (This
is inconsistent with functions such as
which always drop attributes, and is for efficiency since lists can be
expensive to copy.)
TRUE if and only if its argument
list or a
length $> 0$.
TRUE if and only if the argument
is a pairlist or
NULL (see below).
"environment" method for
as.list copies the
name-value pairs (for names not beginning with a dot) from an
environment to a named list. The user can request that all named
objects are copied. Unless
sorted = TRUE, the list is in no
particular order (the order
depends on the order of creation of objects and whether the
environment is hashed). No enclosing environments are searched.
(Objects copied are duplicated so this can be an expensive operation.)
Note that there is an inverse operation, the
as.environment() method for list objects.
An empty pairlist,
pairlist() is the same as
NULL. This is different from
as.pairlist is implemented as
"pairlist"), and hence will dispatch methods for the generic function
as.vector. Lists are copied element-by-element into a pairlist
and the names of the list used as tags for the pairlist: the return
value for other types of argument is undocumented.
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth & Brooks/Cole.
plotmath for the use of
list in plot annotation.
require(graphics) # create a plotting structure pts <- list(x = cars[,1], y = cars[,2]) plot(pts) is.pairlist(.Options) # a user-level pairlist ## "pre-allocate" an empty list of length 5 vector("list", 5) # Argument lists f <- function() x # Note the specification of a "..." argument: formals(f) <- al <- alist(x = , y = 2+3, ... = ) f al ## environment->list coercion e1 <- new.env() e1$a <- 10 e1$b <- 20 as.list(e1)
Example file for [LinkedIn Learning: R for Data Science: Lunchbreak lessons](https://linkedin-learning.pxf.io/rweekly_lists). ```r # lists are special types of vectors, but store mixed types: I.am.a.vector <- c(1,TRUE,"gyre") I.am.a.list <- list(1,TRUE,"gyre") str(I.am.a.vector) # character str(I.am.a.list) # mixed # lists can contain lists a.list <- list(letters[1:3]) # contains "a", "b", "c" another.list <- list(1:5) # contains 1,2,3,4,5 still.another.list <- list(TRUE,FALSE,TRUE) I.am.a.list <- list(a.list,another.list,still.another.list) str(I.am.a.list) # named list elements (aka key/value) I.am.a.list <- list(bob=c(6.2,150),bill=c(5.4,110)) names(I.am.a.list) I.am.a.list["bob"] I.am.a.list$bob I.am.a.list$bob # see also # https://cran.r-project.org/doc/manuals/r-release/R-lang.html#List-objects ```