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)

