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

