home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / NEXTSTEP / UNIX / Educational / R-0.49-MI / R-0.49-I / help / survival4 / rowsum < prev    next >
Encoding:
Text File  |  1997-09-13  |  1.2 KB  |  41 lines

  1.     
  2.     _G_i_v_e _r_o_w _s_u_m_s _o_f _a _m_a_t_r_i_x, _b_a_s_e_d _o_n _a _g_r_o_u_p_i_n_g _v_a_r_i_a_b_l_e.
  3.     
  4.          rowsum(x, group, reorder=T)
  5.     
  6.     _A_r_g_u_m_e_n_t_s:
  7.     
  8.                 x:
  9.           a matrix or vector of numeric data.  Missing values
  10.          are allowed.
  11.     
  12.             group:
  13.           a vector giving the grouping, with one element per row
  14.          of x.  Missing values are not allowed.
  15.     
  16.           reorder:
  17.          if True, then the result will be in order of
  18.          sort(unique(group)), if False, it will be in the order
  19.          that rows were encountered (and may run faster for
  20.          large matrices).  The default is to reorder the data,
  21.          so as to agree with tapply (see example below).
  22.     
  23.          Value:
  24.     
  25.          a matrix containing the sums.  There will be one row
  26.          per unique value of group.
  27.     
  28.          tapply
  29.     
  30.     _E_x_a_m_p_l_e_s:
  31.     
  32.          x <- matrix(runif(100), ncol=5)
  33.          group <- sample(1:8, 20, T)
  34.          xsum <- rowsum(x, group)
  35.          #same result another way, slower, and temp may be much larger than x
  36.          temp <- model.matrix( ~a -1, data.frame(a=as.factor(group)))
  37.          xsum2<- t(temp) %*% x
  38.          #same as last one, but really slow
  39.          xsum3 <- tapply(x, list(group[row(x)], col(x)), sum)
  40.     
  41.