Extract or Replace Parts of an Object - Generic operator

Extract parts of a vector, a list, or an array. If to the left of an assignment, then that portion of the object is changed.

This function is generic (see Methods); method functions can be written to handle specific classes of data. Classes which already have methods for this function include: anova, data.frame, factor, model.matrix, smooth, tree.

x[i, j, ...]
x[i, j, ..., drop=T]
x[[i, j, ...]]

any object.
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.
a name or quoted string.

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 - an expression m[,i,drop=F] will always produce a 2-dimensional matrix regardless of the length of i.

These are the extraction functions, returning some elements or properties of an object.

They may also appear on the left of an assignment operation, to carry out replacement in an object.

Vector subscripts are generated with when i and x are both vectors. (Note that an array is also a vector, hence arrays can be subscripted as vectors - both intentionally and unintentionally.) The result of the expression is to extract or replace elements of x corresponding to a vector of positive indices computed according to the value of i.

If i is empty, all of x is extracted or replaced, without affecting the attributes of x. If i is logical the indices are produced by starting at and selecting the numbers for which the corresponding element is If is shorter than it is extended by cyclic repetition. It can be longer than as well, with no change in the computation of indices. If has mode indices are determined by matching the elements of against the attribute Unmatched names (including the case that there is no such attribute) index outside the current length of x. If is numeric, and the indices consist of the elements of that do not match any elements in Otherwise, itself is taken to be the indices. The indices can have any positive values, or Zeros are dropped before using the indices.

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 and the same length as the number of indices. The elements of are the elements of corresponding to the indices, except if the indices are greater than the length of or are In either of those exceptions the returned elements are that is, for an atomic mode ("" for character) and for a non-atomic mode. All the attributes of will be discarded in the subset, except for the attribute. The names attribute of will be names(x)[i].

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.

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.

A special case of the [[ construct is when x is recursive (e.g., a list) and i is a list. The components of i should all have length 1 and be character or numeric. The statement x[[i]] is equivalent to: x [[ i[[1]] ]] [[ i[[2]] ]] ... [[ i[[length(i)]] ]] For example, x[[list("b", 2, 4)]] will pick out the 4th component of the 2nd component of the b component of x.

The expression x$name is the name component of x. It is equivalent to x[["name"]] if x is recursive and NULL otherwise. Partial matching is performed on the names of x when assignment or replacement is not being done; thus to extract a component of a list, you only need to give enough of the name to make it unique. Replacement of the name component may coerce an object to be a list.

For replacements, x[i] <- value the rule is that the length of will be set to the largest value in the indices, if that is bigger than the current length of If and 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.

To replace a component of a list you must give the entire name of the component. If you abbreviate the name (as you can when merely extracting it), then a new component with the abbreviated name will be added.

attr , attributes , Comparison , Syntax .

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[] # identical to A 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

al <- as.list(1:4)

al[[3]] <- NULL # remove third component - length of al is reduced by 1

al[2:3] <- list(NULL) # replace 2nd and 3rd component by NULL