rowsum                 package:base                 R Documentation

_G_i_v_e _r_o_w _s_u_m_s _o_f _a _m_a_t_r_i_x _o_r _d_a_t_a _f_r_a_m_e, _b_a_s_e_d _o_n _a _g_r_o_u_p_i_n_g _v_a_r_i_a_b_l_e

_D_e_s_c_r_i_p_t_i_o_n:

     Compute sums across rows of a matrix-like object for each level of
     a grouping variable. 'rowsum' is generic, with methods for
     matrices and data frames.

_U_s_a_g_e:

     rowsum(x, group, reorder = TRUE, ...)

_A_r_g_u_m_e_n_t_s:

       x: a matrix, data frame or vector of numeric data.  Missing
          values are    allowed.

   group: a vector giving the grouping, with one element per row of
          'x'.  Missing values will be treated as another group and a
          warning will be given

 reorder: if 'TRUE', then the result will be in order of
          'sort(unique(group))', if 'FALSE', it will be in the order
          that rows were encountered. 

     ...: other arguments for future methods

_D_e_t_a_i_l_s:

     The default is to reorder the rows to agree with 'tapply' as in
     the example below. Reordering should not add noticeably to the
     time except when there are very many distinct values of 'group'
     and 'x' has few columns.

     The original function was written by Terry Therneau, but this is a
     new implementation using hashing that is much faster for large
     matrices.

     To add all the rows of a matrix (ie, a single 'group') use
     'rowSums', which should be even faster.

_V_a_l_u_e:

     a matrix or data frame containing the sums.  There will be one row
     per unique value  of 'group'.

_S_e_e _A_l_s_o:

     'tapply', 'aggregate', 'rowSums'

_E_x_a_m_p_l_e_s:

     x <- matrix(runif(100), ncol=5)
     group <- sample(1:8, 20, TRUE)
     xsum <- rowsum(x, group)
     ## Slower versions
     xsum2 <- tapply(x, list(group[row(x)], col(x)), sum)
     xsum3<- aggregate(x,list(group),sum)

