Rev 71883 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
% File src/library/base/man/array.Rd% Part of the R package, https://www.R-project.org% Copyright 1995-2017 R Core Team% Distributed under GPL 2 or later\name{array}\alias{array}\alias{as.array}\alias{as.array.default}\alias{is.array}\title{Multi-way Arrays}\description{Creates or tests for arrays.}\usage{array(data = NA, dim = length(data), dimnames = NULL)as.array(x, ...)is.array(x)}\arguments{\item{data}{a vector (including a list or \code{\link{expression}}vector) giving data to fill the array. Non-atomic classed objectsare coerced by \code{\link{as.vector}}.}\item{dim}{the dim attribute for the array to be created, that is aninteger vector of length one or more giving the maximal indices ineach dimension.}\item{dimnames}{either \code{NULL} or the names for the dimensions.This must a list (or it will be ignored) with one component for eachdimension, either \code{NULL} or a character vector of the lengthgiven by \code{dim} for that dimension. The list can be named, andthe list names will be used as names for the dimensions. If thelist is shorter than the number of dimensions, it is extended by\code{NULL}s to the length required.}\item{x}{an \R object.}\item{\dots}{additional arguments to be passed to or from methods.}}\details{An array in \R can have one, two or more dimensions. It is simply avector which is stored with additional \link{attributes} giving thedimensions (attribute \code{"dim"}) and optionally names for thosedimensions (attribute \code{"dimnames"}).A two-dimensional array is the same thing as a \code{\link{matrix}}.One-dimensional arrays often look like vectors, but may be handleddifferently by some functions: \code{\link{str}} does distinguishthem in recent versions of \R.The \code{"dim"} attribute is an integer vector of length one or morecontaining non-negative values: the product of the values must matchthe length of the array.The \code{"dimnames"} attribute is optional: if present it is a listwith one component for each dimension, either \code{NULL} or acharacter vector of the length given by the element of the\code{"dim"} attribute for that dimension.\code{is.array} is a \link{primitive} function.For a list array, the \code{print} methods prints entries of lengthnot one in the form \samp{integer,7} indicating the type and length.}\value{\code{array} returns an array with the extents specified in \code{dim}and naming information in \code{dimnames}. The values in \code{data} aretaken to be those in the array with the leftmost subscript movingfastest. If there are too few elements in \code{data} to fill the array,then the elements in \code{data} are recycled. If \code{data} haslength zero, \code{NA} of an appropriate type is used for atomicvectors (\code{0} for raw vectors) and \code{NULL} for lists.Unlike \code{\link{matrix}}, \code{array} does not currently removeany attributes left by \code{as.vector} from a classed list\code{data}, so can return a list array with a class attribute.\code{as.array} is a generic function for coercing to arrays. Thedefault method does so by attaching a \code{\link{dim}} attribute toit. It also attaches \code{\link{dimnames}} if \code{x} has\code{\link{names}}. The sole purpose of this is to make it possibleto access the \code{dim[names]} attribute at a later time.\code{is.array} returns \code{TRUE} or \code{FALSE} depending onwhether its argument is an array (i.e., has a \code{dim} attribute ofpositive length) or not. It is generic: you can write methods to handlespecific classes of objects, see \link{InternalMethods}.}\note{\code{is.array} is a \link{primitive} function.}\references{Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)\emph{The New S Language}.Wadsworth & Brooks/Cole.}\seealso{\code{\link{aperm}}, \code{\link{matrix}},\code{\link{dim}}, \code{\link{dimnames}}.}\examples{dim(as.array(letters))array(1:3, c(2,4)) # recycle 1:3 "2 2/3 times"# [,1] [,2] [,3] [,4]#[1,] 1 3 2 1#[2,] 2 1 3 2}\keyword{array}