R apply function with multiple parameters
r
Solution
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){
var1*var2
}
var2 <- 2
sapply(mylist,myfxn,var2=var2)
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.
Problem
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 ``` [f(L[1],1),f(L[2],1),...,f(L[n],1)] ``` How do I do this with either `apply`, `mapply` or `lapply`?