Rev 87291 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
% File src/library/base/man/list.Rd% Part of the R package, https://www.R-project.org% Copyright 1995-2024 R Core Team% Distributed under GPL 2 or later\name{list}\alias{list}\alias{pairlist}\alias{alist}\alias{as.list}\alias{as.list.default}\alias{as.list.data.frame}\alias{as.list.environment}\alias{as.list.factor}\alias{as.list.function}\alias{as.pairlist}\alias{is.list}\alias{is.pairlist}\title{Lists -- Generic and Dotted Pairs}\description{Functions to construct, coerce and check for both kinds of \R lists.}\usage{list(\dots)pairlist(\dots)as.list(x, \dots)\method{as.list}{environment}(x, all.names = FALSE, sorted = FALSE, \dots)as.pairlist(x)is.list(x)is.pairlist(x)alist(\dots)}\arguments{\item{\dots}{objects, possibly named.}\item{x}{object to be coerced or tested.}\item{all.names}{a logical indicating whether to copy all values or(default) only those whose names do not begin with a dot.}\item{sorted}{a logical indicating whether the \code{\link{names}} ofthe resulting list should be sorted (increasingly). Note that thisis somewhat costly, but may be useful for comparison of environments.}}\details{Almost all lists in \R internally are \emph{Generic Vectors}, whereastraditional \emph{dotted pair} lists (as in LISP) remain available butrarely seen by users (except as \code{\link{formals}} of functions).The arguments to \code{list} or \code{pairlist} are of the form\code{value} or \code{tag = value}. The functions return a list ordotted pair list composed of its arguments with each value eithertagged or untagged, depending on how the argument was specified.\code{alist} handles its arguments as if they described functionarguments. So the values are not evaluated, and tagged arguments withno value are allowed whereas \code{list} simply ignores them.\code{alist} is most often used in conjunction with \code{\link{formals}}.\code{as.list} attempts to coerce its argument to a list. Forfunctions, this returns the concatenation of the list of formalarguments and the function body. For expressions, the list ofconstituent elements is returned. \code{as.list} is generic, and asthe default method calls \code{\link{as.vector}(mode = "list")} for anon-list, methods for \code{as.vector} may be invoked. \code{as.list}turns a factor into a list of one-element factors, keeping\code{\link{names}}. Other attributes maybe dropped unless the argument already is a list or expression. (Thisis inconsistent with functions such as \code{\link{as.character}}which always drop attributes, and is for efficiency since lists can beexpensive to copy.)\code{is.list} returns \code{TRUE} if and only if its argumentis a \code{list} \emph{or} a \code{pairlist} of \code{length} \eqn{> 0}.\code{is.pairlist} returns \code{TRUE} if and only if the argumentis a pairlist (or \code{NULL} which \emph{is} a pairlist, see below).The \code{"\link{environment}"} method for \code{as.list} copies thename-value pairs (for names not beginning with a dot) from anenvironment to a named list. The user can request that all namedobjects are copied. Unless \code{sorted = TRUE}, the list is in noparticular order (the orderdepends on the order of creation of objects and whether theenvironment 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\code{\link{as.environment}()} method for list objects.An empty pairlist, \code{pairlist()} is the same as\code{\link{NULL}}. This is different from \code{list()}: some butnot all operations will promote an empty pairlist to an empty list.\code{as.pairlist} is implemented as \code{\link{as.vector}(x,"pairlist")}, and hence will dispatch methods for the generic function\code{as.vector}. Lists are copied element-by-element into a pairlistand the names of the list used as tags for the pairlist: the returnvalue for other types of argument is undocumented.\code{list}, \code{is.list} and \code{is.pairlist} are\link{primitive} functions.}\references{\bibshow{R:Becker+Chambers+Wilks:1988}}\seealso{\code{\link{vector}("list", length)} for creation of a list with emptycomponents; \code{\link{c}}, for concatenation; \code{\link{formals}}.\code{\link{unlist}} is an approximate inverse to \code{as.list()}.\sQuote{\link{plotmath}} for the use of \code{list} in plot annotation.}\examples{require(graphics)# create a plotting structurepts <- list(x = cars[,1], y = cars[,2])plot(pts)is.pairlist(.Options) # a user-level pairlist## "pre-allocate" an empty list of length 5vector("list", 5)# Argument listsf <- function() x# Note the specification of a "..." argument:formals(f) <- al <- alist(x = , y = 2+3, \dots = )fal## environment->list coercione1 <- new.env()e1$a <- 10e1$b <- 20as.list(e1)}\keyword{list}\keyword{manip}