o handle indexing of
specific classes of objects, see [ InternalMethods
](/link/InternalMethods?package=base&version=3.6.2) as well as ` [
[.data.frame ](/link/%5B.data.frame?package=base&version=3.6.2) ` and ` [
[.factor ](/link/%5B.factor?package=base&version=3.6.2) ` . The descriptions
here apply only to the default methods. Note that separate methods are
required for the replacement functions ` [<- ` , ` [[<- ` and ` $<- ` for use
when indexing occurs on the assignment side of an expression.

The most important distinction between ` [ ` , ` [[ ` and ` $ ` is that the `
[ ` can select more than one element whereas the other two select a single
element.

The default methods work somewhat differently for atomic vectors,
matrices/arrays and for recursive (list-like, see ` [ is.recursive
](/link/is.recursive?package=base&version=3.6.2) ` ) objects. ` $ ` is only
valid for recursive objects, and is only discussed in the section below on
recursive objects.

Subsetting (except by an empty index) will drop all attributes except ` names
` , ` dim ` and ` dimnames ` .

Indexing can occur on the right-hand-side of an expression for extraction, or
on the left-hand-side for replacement. When an index expression appears on the
left side of an assignment (known as _subassignment_ ) then that part of ` x `
is set to the value of the right hand side of the assignment. In this case no
partial matching of character indices is done, and the left-hand-side is
coerced as needed to accept the values. For vectors, the answer will be of the
higher of the types of ` x ` and ` value ` in the hierarchy raw < logical <
integer < double < complex < character < list < expression. Attributes are
preserved (although ` names ` , ` dim ` and ` dimnames ` will be adjusted
suitably). Subassignment is done sequentially, so if an index is specified
more than once the latest assigned value for an index will result.

It is an error to apply any of these operators to an object which is not
subsettable (e.g., a function).

##  References

Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) _The New S Language_ .
Wadsworth & Brooks/Cole.

##  See Also

` [ names ](/link/names?package=base&version=3.6.2) ` for details of matching
to names, and ` [ pmatch ](/link/pmatch?package=base&version=3.6.2) ` for
partial matching.

` [ list ](/link/list?package=base&version=3.6.2) ` , ` [ array
](/link/array?package=base&version=3.6.2) ` , ` [ matrix
](/link/matrix?package=base&version=3.6.2) ` .

` [ [.data.frame ](/link/%5B.data.frame?package=base&version=3.6.2) ` and ` [
[.factor ](/link/%5B.factor?package=base&version=3.6.2) ` for the behaviour
when applied to data.frame and factors.

` [ Syntax ](/link/Syntax?package=base&version=3.6.2) ` for operator
precedence, and the ‘R Language Definition’ manual about indexing details.

` [ NULL ](/link/NULL?package=base&version=3.6.2) ` for details of indexing
null objects.

##  Examples

[ Run this code
](https://app.datacamp.com/workspace/preview?_tag=rdocs&rdocsPath=packages%2Fbase%2Fversions%2F3.6.2%2Ftopics%2FExtract&utm_source=r-docs&utm_medium=docs&utm_term=Extract&utm_content=run_example_in_workspace)

    
    
    # NOT RUN {
    x <- 1:12
    m <- matrix(1:6, nrow = 2, dimnames = list(c("a", "b"), LETTERS[1:3]))
    li <- list(pi = pi, e = exp(1))
    x[10]                 # the tenth element of x
    x <- x[-1]            # delete the 1st element of x
    m[1,]                 # the first row of matrix m
    m[1, , drop = FALSE]  # is a 1-row matrix
    m[,c(TRUE,FALSE,TRUE)]# logical indexing
    m[cbind(c(1,2,1),3:1)]# matrix numeric index
    ci <- cbind(c("a", "b", "a"), c("A", "C", "B"))
    m[ci]                 # matrix character index
    m <- m[,-1]           # delete the first column of m
    li[[1]]               # the first element of list li
    y <- list(1, 2, a = 4, 5)
    y[c(3, 4)]            # a list containing elements 3 and 4 of y
    y$a                   # the element of y named a
    
    ## non-