The R Project SVN R

Rev

Rev 87256 | 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
68948 ripley 2
% Part of the R package, https://www.R-project.org
71883 ripley 3
% Copyright 1995-2017 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.
81118 maechler 28
    This must be a list (or it will be ignored) with one component for each
59619 ripley 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.
63806 ripley 59
 
87256 smeyer 60
  For a list array, the \code{print} method prints entries of length
63806 ripley 61
  not one in the form \samp{integer,7} indicating the type and length.
51217 ripley 62
}
2 r 63
\value{
7782 hornik 64
  \code{array} returns an array with the extents specified in \code{dim}
15805 ripley 65
  and naming information in \code{dimnames}.  The values in \code{data} are
7782 hornik 66
  taken to be those in the array with the leftmost subscript moving
15805 ripley 67
  fastest.  If there are too few elements in \code{data} to fill the array,
28014 ripley 68
  then the elements in \code{data} are recycled.  If \code{data} has
69
  length zero, \code{NA} of an appropriate type is used for atomic
30058 ripley 70
  vectors (\code{0} for raw vectors) and \code{NULL} for lists.
2 r 71
 
59708 ripley 72
  Unlike \code{\link{matrix}}, \code{array} does not currently remove
73
  any attributes left by \code{as.vector} from a classed list
59712 ripley 74
  \code{data}, so can return a list array with a class attribute.
59708 ripley 75
 
45616 hornik 76
  \code{as.array} is a generic function for coercing to arrays.  The
77
  default method does so by attaching a \code{\link{dim}} attribute to
78
  it.  It also attaches \code{\link{dimnames}} if \code{x} has
79
  \code{\link{names}}.  The sole purpose of this is to make it possible
51217 ripley 80
  to access the \code{dim[names]} attribute at a later time.
2 r 81
 
7782 hornik 82
  \code{is.array} returns \code{TRUE} or \code{FALSE} depending on
40675 ripley 83
  whether its argument is an array (i.e., has a \code{dim} attribute of
84
  positive length) or not.  It is generic: you can write methods to handle
27625 ripley 85
  specific classes of objects, see \link{InternalMethods}.
2 r 86
}
48662 ripley 87
\note{
51315 ripley 88
  \code{is.array} is a \link{primitive} function.
48662 ripley 89
}
24300 ripley 90
\references{
88581 hornik 91
  \bibshow{R:Becker+Chambers+Wilks:1988}
24300 ripley 92
}
2 r 93
\seealso{
10525 ripley 94
  \code{\link{aperm}}, \code{\link{matrix}},
95
  \code{\link{dim}}, \code{\link{dimnames}}.
2 r 96
}
97
\examples{
98
dim(as.array(letters))
7495 maechler 99
array(1:3, c(2,4)) # recycle 1:3 "2 2/3 times"
7501 tlumley 100
#     [,1] [,2] [,3] [,4]
101
#[1,]    1    3    2    1
102
#[2,]    2    1    3    2
2 r 103
}
286 maechler 104
\keyword{array}