mapply package:base R Documentation _A_p_p_l_y _a _f_u_n_c_t_i_o_n _t_o _m_u_l_t_i_p_l_e _l_i_s_t _o_r _v_e_c_t_o_r _a_r_g_u_m_e_n_t_s _D_e_s_c_r_i_p_t_i_o_n: 'mapply' is a multivariate version of 'sapply'. 'mapply' applies 'FUN' to the first elements of each ... argument, the second elements, the third elements, and so on. Arguments are recycled if necessary. 'Vectorize' returns a new function that acts as if 'mapply' was called. _U_s_a_g_e: mapply(FUN, ..., MoreArgs = NULL, SIMPLIFY = TRUE, USE.NAMES = TRUE) Vectorize(FUN, vectorize.args = arg.names, SIMPLIFY = TRUE, USE.NAMES = TRUE) _A_r_g_u_m_e_n_t_s: FUN: function to apply, found via 'match.fun'. ...: arguments to vectorize over (list or vector). MoreArgs: a list of other arguments to 'FUN'. SIMPLIFY: logical; attempt to reduce the result to a vector or matrix? USE.NAMES: logical; use names if the first ... argument has names, or if it is a character vector, use that character vector as the names. vectorize.args: a character vector of arguments which should be vectorized. Defaults to all arguments to 'FUN'. _D_e_t_a_i_l_s: The arguments named in the 'vectorize.args' argument to 'Vectorize' correspond to the arguments passed in the '...' list to 'mapply'. However, only those that are actually passed will be vectorized; default values will not. See the example below. 'Vectorize' cannot be used with primitive functions as they have no formal list. _V_a_l_u_e: 'mapply' returns a list, vector, or matrix. 'Vectorize' returns a function with the same arguments as 'FUN', but wrapping a call to 'mapply'. _S_e_e _A_l_s_o: 'sapply', 'outer' _E_x_a_m_p_l_e_s: require(graphics) mapply(rep, 1:4, 4:1) mapply(rep, times=1:4, x=4:1) mapply(rep, times=1:4, MoreArgs=list(x=42)) # Repeat the same using Vectorize: use rep.int as rep is primitive vrep <- Vectorize(rep.int) vrep(1:4, 4:1) vrep(times=1:4, x=4:1) vrep <- Vectorize(rep.int, "times") vrep(times=1:4, x=42) mapply(function(x,y) seq_len(x) + y, c(a= 1, b=2, c= 3), # names from first c(A=10, B=0, C=-10)) word <- function(C,k) paste(rep.int(C,k), collapse='') utils::str(mapply(word, LETTERS[1:6], 6:1, SIMPLIFY = FALSE)) f <- function(x=1:3, y) c(x,y) vf <- Vectorize(f, SIMPLIFY = FALSE) f(1:3,1:3) vf(1:3,1:3) vf(y=1:3) # Only vectorizes y, not x # Nonlinear regression contour plot, based on nls() example SS <- function(Vm, K, resp, conc) { pred <- (Vm * conc)/(K + conc) sum((resp - pred)^2 / pred) } vSS <- Vectorize(SS, c("Vm", "K")) Treated <- subset(Puromycin, state == "treated") Vm <- seq(140, 310, len=50) K <- seq(0, 0.15, len=40) SSvals <- outer(Vm, K, vSS, Treated$rate, Treated$conc) contour(Vm, K, SSvals, levels=(1:10)^2, xlab="Vm", ylab="K")