subset                 package:base                 R Documentation

_S_u_b_s_e_t_t_i_n_g _V_e_c_t_o_r_s, _M_a_t_r_i_c_e_s _a_n_d _D_a_t_a _F_r_a_m_e_s

_D_e_s_c_r_i_p_t_i_o_n:

     Return subsets of vectors, matrices or data frames which meet
     conditions.

_U_s_a_g_e:

     subset(x, ...)

     ## Default S3 method:
     subset(x, subset, ...)

     ## S3 method for class 'matrix':
     subset(x, subset, select, drop = FALSE, ...)

     ## S3 method for class 'data.frame':
     subset(x, subset, select, drop = FALSE, ...)

_A_r_g_u_m_e_n_t_s:

       x: object to be subsetted.

  subset: logical expression indicating elements or rows to keep:
          missing values are taken as false.

  select: expression, indicating columns to select from a data frame.

    drop: passed on to '[' indexing operator.

     ...: further arguments to be passed to or from other methods.

_D_e_t_a_i_l_s:

     This is a generic function, with methods supplied for matrices,
     data frames and vectors (including lists).  Packages and users can
     add further methods.

     For ordinary vectors, the result is simply 'x[subset &
     !is.na(subset)]'.

     For data frames, the 'subset' argument works on the rows.  Note
     that 'subset' will be evaluated in the data frame, so columns can
     be referred to (by name) as variables in the expression (see the
     examples).

     The 'select' argument exists only for the methods for data frames
     and matrices.  It works by first replacing column names in the
     selection expression with the corresponding column numbers in the
     data frame and then using the resulting integer vector to index
     the columns.  This allows the use of the standard indexing
     conventions so that for example ranges of columns can be specified
     easily, or single columns can be dropped (see the examples).

     The 'drop' argument is passed on to the indexing method for
     matrices and data frames: note that the default for matrices is
     different from that for indexing.

_V_a_l_u_e:

     An object similar to 'x' contain just the selected elements (for a
     vector), rows and columns (for a matrix or data frame), and so on.

_A_u_t_h_o_r(_s):

     Peter Dalgaard and Brian Ripley

_S_e_e _A_l_s_o:

     '[',  'transform'

_E_x_a_m_p_l_e_s:

     subset(airquality, Temp > 80, select = c(Ozone, Temp))
     subset(airquality, Day == 1, select = -Temp)
     subset(airquality, select = Ozone:Wind)

     with(airquality, subset(Ozone, Temp > 80))

     ## sometimes requiring a logical 'subset' argument is a nuisance
     nm <- rownames(state.x77)
     start_with_M <- nm %in% grep("^M", nm, value=TRUE)
     subset(state.x77, start_with_M, Illiteracy:Murder)

