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

_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 row 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.

_S_e_e _A_l_s_o:

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

_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

