The R Project SVN R

Rev

Rev 61282 | Rev 68948 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
42333 ripley 1
% File src/library/base/man/array.Rd
2
% Part of the R package, http://www.R-project.org
59619 ripley 3
% Copyright 1995-2012 R Core Team
42333 ripley 4
% Distributed under GPL 2 or later
5
 
2 r 6
\name{array}
56186 murdoch 7
\alias{array}
7782 hornik 8
\alias{as.array}
45616 hornik 9
\alias{as.array.default}
7782 hornik 10
\alias{is.array}
2 r 11
\title{Multi-way Arrays}
7782 hornik 12
\description{
13
  Creates or tests for arrays.
14
}
2 r 15
\usage{
15805 ripley 16
array(data = NA, dim = length(data), dimnames = NULL)
45616 hornik 17
as.array(x, ...)
2 r 18
is.array(x)
19
}
10525 ripley 20
\arguments{
53724 ripley 21
  \item{data}{a vector (including a list or \code{\link{expression}}
59708 ripley 22
    vector) giving data to fill the array.  Non-atomic classed objects
23
    are coerced by \code{\link{as.vector}}.}
24
  \item{dim}{the dim attribute for the array to be created, that is an
25
    integer vector of length one or more giving the maximal indices in
15805 ripley 26
    each dimension.}
59619 ripley 27
  \item{dimnames}{either \code{NULL} or the names for the dimensions.
28
    This must a list (or it will be ignored) with one component for each
29
    dimension, either \code{NULL} or a character vector of the length
30
    given by \code{dim} for that dimension.  The list can be named, and
31
    the list names will be used as names for the dimensions.  If the
32
    list is shorter than the number of dimensions, it is extended by
33
    \code{NULL}s to the length required.}
47067 ripley 34
  \item{x}{an \R object.}
45616 hornik 35
  \item{\dots}{additional arguments to be passed to or from methods.}
10525 ripley 36
}
51217 ripley 37
\details{
38
  An array in \R can have one, two or more dimensions.  It is simply a
39
  vector which is stored with additional \link{attributes} giving the
40
  dimensions (attribute \code{"dim"}) and optionally names for those
41
  dimensions (attribute \code{"dimnames"}).
42
 
43
  A two-dimensional array is the same thing as a \code{\link{matrix}}.
61433 ripley 44
 
51217 ripley 45
  One-dimensional arrays often look like vectors, but may be handled
46
  differently by some functions: \code{\link{str}} does distinguish
47
  them in recent versions of \R.
48
 
49
  The \code{"dim"} attribute is an integer vector of length one or more
50
  containing non-negative values: the product of the values must match
51
  the length of the array.
52
 
53
  The \code{"dimnames"} attribute is optional: if present it is a list
54
  with one component for each dimension, either \code{NULL} or a
55
  character vector of the length given by the element of the
61433 ripley 56
  \code{"dim"} attribute for that dimension.
57
 
51318 ripley 58
  \code{is.array} is a \link{primitive} function.
51217 ripley 59
}
2 r 60
\value{
7782 hornik 61
  \code{array} returns an array with the extents specified in \code{dim}
15805 ripley 62
  and naming information in \code{dimnames}.  The values in \code{data} are
7782 hornik 63
  taken to be those in the array with the leftmost subscript moving
15805 ripley 64
  fastest.  If there are too few elements in \code{data} to fill the array,
28014 ripley 65
  then the elements in \code{data} are recycled.  If \code{data} has
66
  length zero, \code{NA} of an appropriate type is used for atomic
30058 ripley 67
  vectors (\code{0} for raw vectors) and \code{NULL} for lists.
2 r 68
 
59708 ripley 69
  Unlike \code{\link{matrix}}, \code{array} does not currently remove
70
  any attributes left by \code{as.vector} from a classed list
59712 ripley 71
  \code{data}, so can return a list array with a class attribute.
59708 ripley 72
 
45616 hornik 73
  \code{as.array} is a generic function for coercing to arrays.  The
74
  default method does so by attaching a \code{\link{dim}} attribute to
75
  it.  It also attaches \code{\link{dimnames}} if \code{x} has
76
  \code{\link{names}}.  The sole purpose of this is to make it possible
51217 ripley 77
  to access the \code{dim[names]} attribute at a later time.
2 r 78
 
7782 hornik 79
  \code{is.array} returns \code{TRUE} or \code{FALSE} depending on
40675 ripley 80
  whether its argument is an array (i.e., has a \code{dim} attribute of
81
  positive length) or not.  It is generic: you can write methods to handle
27625 ripley 82
  specific classes of objects, see \link{InternalMethods}.
2 r 83
}
48662 ripley 84
\note{
51315 ripley 85
  \code{is.array} is a \link{primitive} function.
59626 ripley 86
 
61282 ripley 87
  \R 2.x.y allowed (although documented not to) a zero-length \code{dim}
88
  argument, and returned a vector of length one.
48662 ripley 89
}
24300 ripley 90
\references{
91
  Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)
92
  \emph{The New S Language}.
47262 ripley 93
  Wadsworth & Brooks/Cole.
24300 ripley 94
}
2 r 95
\seealso{
10525 ripley 96
  \code{\link{aperm}}, \code{\link{matrix}},
97
  \code{\link{dim}}, \code{\link{dimnames}}.
2 r 98
}
99
\examples{
100
dim(as.array(letters))
7495 maechler 101
array(1:3, c(2,4)) # recycle 1:3 "2 2/3 times"
7501 tlumley 102
#     [,1] [,2] [,3] [,4]
103
#[1,]    1    3    2    1
104
#[2,]    2    1    3    2
2 r 105
}
286 maechler 106
\keyword{array}