Take the 2-minute tour ×

I have a function f(var1, var2) in R. Suppose we set var2 = 1 and now I want to apply the function f() to the list L. Basically I want to get a new list L* with the outputs


How do I do this with either apply, mapply or lapply?

share|improve this question
add comment

1 Answer

up vote 28 down vote accepted

Just pass var2 as an extra argument to one of the apply functions.

mylist <- list(a=1,b=2,c=3)
myfxn <- function(var1,var2){
var2 <- 2


This passes the same var2 to every call of myfxn. If instead you want each call of myfxn to get the 1st/2nd/3rd/etc. element of both mylist and var2, then you're in mapply's domain.

share|improve this answer
but note that myfxn may be vectorised, in which case one should use myfxn(unlist(mylist), var2=var2) –  baptiste Jul 26 '11 at 21:08
The original example was unclear but seemed to be to be non-vectorized. Point well taken, however. –  Ari B. Friedman Jul 26 '11 at 21:14
add comment

Your Answer


