by                   package:base                   R Documentation

_A_p_p_l_y _a _F_u_n_c_t_i_o_n _t_o _a _D_a_t_a _F_r_a_m_e _s_p_l_i_t _b_y _F_a_c_t_o_r_s

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

     Function 'by' is an object-oriented wrapper for 'tapply' applied
     to data frames.

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

     by(data, INDICES, FUN, ..., simplify = TRUE)

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

    data: an R object, normally a data frame, possibly a matrix.

 INDICES: a factor or a list of factors, each of length 'nrow(data)'.

     FUN: a function to be applied to data frame subsets of 'data'.

     ...: further arguments to 'FUN'.

simplify: logical: see 'tapply'.

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

     A data frame is split by row into data frames subsetted by the
     values of one or more factors, and function 'FUN' is applied to
     each subset in turn.

     Object 'data' will be coerced to a data frame by the default
     method, _but_ if this results in a 1-column data frame, the
     objects passed to 'FUN' are dropped to a subsets of that column.
     (This was the long-term behaviour, but only documented since R
     2.7.0.)

_V_a_l_u_e:

     An object of class '"by"', giving the results for each subset.
     This is always a list if 'simplify' is false, otherwise a list or
     array (see 'tapply').

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

     'tapply'

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

     require(stats)
     attach(warpbreaks)
     by(warpbreaks[, 1:2], tension, summary)
     by(warpbreaks[, 1], list(wool = wool, tension = tension), summary)
     by(warpbreaks, tension, function(x) lm(breaks ~ wool, data = x))

     ## now suppose we want to extract the coefficients by group
     tmp <- by(warpbreaks, tension, function(x) lm(breaks ~ wool, data = x))
     sapply(tmp, coef)

     detach("warpbreaks")

