home *** CD-ROM | disk | FTP | other *** search
-
- _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
-
- apply(x, MARGIN, FUN, ...)
-
- _A_r_g_u_m_e_n_t_s:
-
- x : the array to be used.
-
- MARGIN : a vector giving the subscripts which the
- function will be applied over. 1 indicates
- rows, 2 indicates columns, c(1,2) indicates
- rows and columns.
-
- FUN : the function to be applied. In the case of
- functions like +, %*%, etc., the function
- name must be quoted.
-
- ... : optional arguments to FUN.
-
- _V_a_l_u_e:
-
- If each call to FUN returns a vector of length n, then
- apply returns an array of dimension c(n,dim(x)[MARGIN])
- if n > 1. If n EQUALS 1, apply returns a vector if
- MARGIN has length 1 and an array of dimension
- dim(x)[MARGIN] otherwise.
-
- _S_e_e _A_l_s_o:
-
- lapply, tapply, sweep.
-
- _E_x_a_m_p_l_e_s:
-
- # Compute row and column sums for a matrix:
- x <- cbind(3, c(4:1,2:5))
- col.sums <- apply(x, 2, sum)
- row.sums <- apply(x, 1, sum)
- rbind(cbind(x, row.sums), c(col.sums, sum(col.sums)))
-
- # Sort the columns of a matrix
- apply(x, 2, sort)
-
-