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, ...)

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

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

_V_a_l_u_e:

     A list of class '"by"', giving the results for each subset.

_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")

