### Name: mapply ### Title: Apply a function to multiple list or vector arguments ### Aliases: mapply Vectorize ### Keywords: manip utilities ### ** Examples 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")