rapply                 package:base                 R Documentation

_R_e_c_u_r_s_i_v_e_l_y _A_p_p_l_y _a _F_u_n_c_t_i_o_n _t_o _a _L_i_s_t

_D_e_s_c_r_i_p_t_i_o_n:

     'rapply' is a recursive version of 'lapply'.

_U_s_a_g_e:

     rapply(object, f, classes = "ANY", deflt = NULL,
            how = c("unlist", "replace", "list"), ...)

_A_r_g_u_m_e_n_t_s:

  object: A list.

       f: A function of a single argument.

 classes: A character vector of 'class' names, or '"ANY"' to match any
          class.

   deflt: The default result (not used if 'how = "replace"').

     how: A character string matching the three possibilities given:
          see 'Details'.

     ...: additional arguments passed to the call to 'f'.

_D_e_t_a_i_l_s:

     This function has two basic modes.  If 'how = "replace"', each
     element of the list which is not itself a list and has a class
     included in 'classes' is replaced by the result of applying 'f' to
     the element.

     If the mode is 'how = "list"' or 'how = "unlist"', the list is
     copied, all non-list elements which have a class included in
     'classes' are replaced by the result of applying 'f' to the
     element and all others are replaced by 'deflt'.  Finally, if 'how
     = "unlist"', 'unlist(recursive = TRUE)' is called on the result.

     The semantics differ in detail from 'lapply': in particular the
     arguments are evaluated before calling the C code.

_V_a_l_u_e:

     If 'how = "unlist"', a vector, otherwise a list of similar
     structure to 'object'.

_R_e_f_e_r_e_n_c_e_s:

     Chambers, J. A. (1998) _Programming with Data_. Springer.
      ('rapply' is only described briefly there.)

_S_e_e _A_l_s_o:

     'lapply', 'dendrapply'.

_E_x_a_m_p_l_e_s:

     X <- list(list(a=pi, b=list(c=1:1)), d="a test")
     rapply(X, function(x) x, how="replace")
     rapply(X, sqrt, classes="numeric", how="replace")
     rapply(X, nchar, classes="character",
            deflt = as.integer(NA), how="list")
     rapply(X, nchar, classes="character",
            deflt = as.integer(NA), how="unlist")
     rapply(X, nchar, classes="character", how="unlist")
     rapply(X, log, classes="numeric", how="replace", base=2)

