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)

