Extract                 package:base                 R Documentation

_E_x_t_r_a_c_t _o_r _R_e_p_l_a_c_e _P_a_r_t_s _o_f _a_n _O_b_j_e_c_t

_D_e_s_c_r_i_p_t_i_o_n:

     Operators act on vectors, arrays, dataframes and lists to extract
     or replace subsets.

_U_s_a_g_e:

     x[i]
     x[i, j, ...]
     x[i, j, ... , drop=TRUE]
     x[[i]]
     x[[i, j, ...]]
     x$name
     .subset(x, ...)
     .subset2(x, ...)

_A_r_g_u_m_e_n_t_s:

       x: object from which to extract elements

i,j,...,name: elements to extract or replace. `i,j' are either
          `numeric' or `character' where `name' must be character or an
          (unquoted) name.  Numeric values are coerced to integer as by
          `as.integer(i)'.

    drop: For data frames, matrices, and arrays.  If `TRUE' the result
          is coerced to the lowest possible dimension (see examples
          below).

_D_e_t_a_i_l_s:

     If one of these expressions appears on the left side of an
     assignment then that part of `x' is set to the value of the right
     hand side of the assignment.

     These operators are generic. You can write methods to handle
     subsetting of specific classes of data.

     The `[[' operator requires all relevant subscripts be supplied.
     With the `[' operator a comma separated blank indicates that all
     entries in that dimension are selected.

     When `[.data.frame' is used for subsetting rows of a `data.frame',
     it returns a dataframe with unique (and non-missing)row names, if
     necessary transforming the names using `make.names( * , unique =
     TRUE)'. See the `swiss' example below.

     When operating on a list, the `[[' operator gives the specified
     element of the list while the `[' operator returns a list with the
     specified element(s) in it.

     The operators `$' and `$<-' do not evaluate their second argument.
      It is translated to a string and that string is used to locate
     the correct component of the first argument.

     The functions `.subset' and `.subset2' are essentially equivalent
     to the `[' and `[[' operators, except that methods dispatch does
     not take place.  This is to avoid expensive unclassing in order to
     apply the default method to an object.  They should not normally
     be invoked by end users.

_S_e_e _A_l_s_o:

     `list', `array', `matrix'.

     `Syntax' for operator precedence, and the R Language reference
     manual about indexing details.

_E_x_a_m_p_l_e_s:

     x <- 1:12; m <- matrix(1:6,nr=2); li <- list(pi=pi, e = exp(1))
     x[10]                 # the tenth element of x
     m[1,]                 # the first row of matrix m
     m[1, , drop = FALSE]  # is a 1-row matrix
     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

     data(swiss)
     swiss[ c(1, 1:2), ]   # duplicate row, unique row names

     ## non-integer indices are truncated:
     (i <- 3.999999999) # "4" is printed
     (1:5)[i]  # 3

