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 / base / apply < prev    next >
Encoding:
Text File  |  1997-04-23  |  1.2 KB  |  44 lines

  1.     
  2.     _A_p_p_l_y _F_u_n_c_t_i_o_n_s _O_v_e_r _A_r_r_a_y _M_a_r_g_i_n_s
  3.     
  4.          apply(x, MARGIN, FUN, ...)
  5.     
  6.     _A_r_g_u_m_e_n_t_s:
  7.     
  8.                x : the array to be used.
  9.     
  10.           MARGIN : a vector giving the subscripts which the
  11.                    function will be applied over.  1 indicates
  12.                    rows, 2 indicates columns, c(1,2) indicates
  13.                    rows and columns.
  14.     
  15.              FUN : the function to be applied.  In the case of
  16.                    functions like +, %*%, etc., the function
  17.                    name must be quoted.
  18.     
  19.              ... : optional arguments to FUN.
  20.     
  21.     _V_a_l_u_e:
  22.     
  23.          If each call to FUN returns a vector of length n, then
  24.          apply returns an array of dimension c(n,dim(x)[MARGIN])
  25.          if n > 1.  If n EQUALS 1, apply returns a vector if
  26.          MARGIN has length 1 and an array of dimension
  27.          dim(x)[MARGIN] otherwise.
  28.     
  29.     _S_e_e _A_l_s_o:
  30.     
  31.          lapply, tapply, sweep.
  32.     
  33.     _E_x_a_m_p_l_e_s:
  34.     
  35.          # Compute row and column sums for a matrix:
  36.          x <- cbind(3, c(4:1,2:5))
  37.          col.sums <- apply(x, 2, sum)
  38.          row.sums <- apply(x, 1, sum)
  39.          rbind(cbind(x, row.sums), c(col.sums, sum(col.sums)))
  40.     
  41.          # Sort the columns of a matrix
  42.          apply(x, 2, sort)
  43.     
  44.