.BG
.FN Subscript
.FN [
.FN [[
.FN $
.TL
Extract or Replace Parts of an Object
.CS
x[i]
x[i, j, ...]
x[i, j, ..., drop=T]
x[[i]]
x[[i, j, ...]]
x$name
.AG x
any object.
.AG "i, j"
subscript expressions, used to identify which elements to extract or
replace.
The expressions may be empty (meaning all possible subscripts), logical,
numeric, or character.
.AG drop
logical flag, should dimensions of length 1 be dropped.
For example, assume you have a 5 by 10 matrix `m'.
`m[,1]' will produce a vector of length 5 and `m[,1,drop=F]'
will produce a 5 by 1 matrix.
The `drop=F' argument is most useful in functions where consistency
is important\(eman expression `m[,i,drop=F]' will always produce
a 2-dimensional matrix regardless of the length of `i'.
.AG name
an name or quoted string.
.RT
These are the
.I extraction
functions, returning some elements or properties of an object.
They may also appear on the left of an assignment operation, to carry out
.I replacement
in an object.
.PP
.I "Vector subscripts"
are generated with
.Co x[i]
when `i' and `x' are both vectors.
The result of the expression is to extract or replace elements of `x'
corresponding to a vector of positive
.I indices
computed according to the value of `i'.
.PP
If `i' is empty, all of `x' is extracted or replaced.
If `i' is logical
the indices are produced by starting at
.Co 1
and selecting the numbers for which the corresponding element
.Co i
is
.Co TRUE .
If
.Co i
is shorter than
.Co length(x) ,
it is extended by cyclic repetition.
It can be longer than
.Co length(x)
as well, with no change in the computation of indices.
If
.Co i
has mode
.Cq character ,
.Px character subscript
indices are determined by matching the elements
of
.Co i
against the attribute
.Co names(x) .
Unmatched names (including the case that there is no such attribute) index
outside the current length of `x'.
If
.Co i
is numeric, and
.Px numeric subscript
.Co all(i<=0)
the indices consist of the elements of 
.Co seq(along=x)
that do not match any elements in
.Co -i .
Otherwise,
.Co i
itself is taken to be the indices.
The indices can have any positive values,
.Co 0 ,
or
.Co NA .
Zeroes are dropped before using the indices.
.PP
The computed indices are used for extraction or replacement.
The rule for extraction is that the value of `x[i]'
has the same mode as
.Co x ,
and the same length as the number of indices.
The elements of
.Co x[i]
are the elements of
.Co x
corresponding to the indices, except if the indices are greater than the
length of
.Co x
or are
.Co NA .
In either of those exceptions the returned elements are
.Cq missing ;
that is,
.Co NA
for an atomic mode and
.Co NULL
for a non-atomic mode.
All the attributes of
.Co x
will be discarded in the subset, except for the
.Co names
attribute.
.Px @names~ attribute
The names attribute of
.Co x[i]
will be `names(x)[i]'.
.PP
For replacements,
.Px "subscripts in" replacement
.Cs
x[i] \(<- value
.Ce
the rule is that the length of
.Co x
will be set to the largest value in the indices, if that is bigger
than the current length of
.Co x .
If
.Co x
and
.Co value
do not have the same mode, then one or the other will be coerced to
the common mode that can represent both without loss of information.
This may mean that replacing elements of an object will change
its mode.
.PP
If there are `k' subscripts and `x' is a `k'-way array, indices
identifying a sub-array of `x'
are computed from the `i'-th subscript with respect to `(1:dim(x))[i]'.
Character subscripts in the `i'-th expression are used relative to
the `i'-th element of `dimnames(x)'.
If `x' is a `k'-way array and the single subscript is a matrix with `k'
columns, vector subscripts, one element per row of `i', are computed in
the same way.
.PP
The computations for `x[[i]]' are identical to the above except that
the expression is required to identify a single element to be extracted
or replaced.
The value returned for extraction is the same as for `x[i]' if `x' is
atomic (e.g., numeric).
If `x' is recursive (e.g., a list) the single extracted element is returned,
not the list of length 1 that `x[i]' would produce.
.PP
The expression `x$name' is the `name'
.I component
of `x'.
It is equivalent to `x[["name"]]' if `x' is recursive
and NULL otherwise.
Replacement of the `name' component may coerce an object to be a list.
.SA
`attr'.
.EX
x[x!=999.999]         # x values not equal to 999.999
x[order(y)]           # sort x by increasing values of y
x[\-c(1,3)]           # all but the first and third
list(1:10,2:3)[2]     # value is list(2:3)
list(1:10,2:3)[[2]]   # value is 2:3
x[2,3] <- 8.4         # change the value of a matrix element
state.x77["Alabama",] # print the row for Alabama
A <- array(1:30, c(5,3,2) )   # array with dimension 5 x 3 x 2
A[1,1,1]              # a scalar, the first data value of A
A[1]                  # the same
A[,1:2,]              # a (5,2,2) array
A[A>3]                # the vector 4:30
lsfit(x,y)$coef       # coefficients from a fitted model
.KW manip
.KW array
.WR
