Reputation: 4205
I have a function with two arguments, as follows:
MyFun = function(y, z){0.5*y + 10*z}
I want z to be fixed at 10, and y to take one of the following 1:10. Using lapply I wrote:
lapply(X = 1:10, FUN = MyFun, z=10)
This does the job but it seems that R understands that y is X because it is the missing argument to MyFun. My question is how can I define y explicitly in the additional arguments. I would like to type something like:
lapply(X = 1:10, FUN = MyFun, y=X, z=10)
which obviously will not work as R will look for X in the general environment. The problem with implicitly defining y is that when I have a function with, let say, 10 fixed arguments and one argument taking the value of X, it will be complicated (for an external reader) to conclude which argument is running on X. Things are even more complicated if the function contains ...
Upvotes: 0
Views: 313
Reputation: 72838
You may do this with Map()
from base (similar to lapply()
)
Map(MyFun, y=1:10, z=10)
or mapply()
also from base, if you want a vector in return (similar to sapply()
)
mapply(MyFun, y=1:10, z=10)
# [1] 100.5 101.0 101.5 102.0 102.5 103.0 103.5 104.0 104.5 105.0
Upvotes: 3