[R] how to pass more than one argument to the function called by lapply?
jim holtman
jholtman at gmail.com
Tue Aug 18 13:33:07 CEST 2009
lapply(yourList, function(x1,x2,x3){x1*x2*x3}, x2=12, x3=34)
?lapply
lapply(X, FUN, ...)
sapply(X, FUN, ..., simplify = TRUE, USE.NAMES = TRUE)
replicate(n, expr, simplify = TRUE)
Arguments
X a vector (atomic or list) or an expressions vector. Other objects
(including classed objects) will be coerced by as.list.
FUN the function to be applied to each element of X: see ‘Details’. In
the case of functions like +, %*%, etc., the function name must be
backquoted or quoted.
... optional arguments to FUN.
Noitce the '...' which are the "optional arguments to FUN".
On Mon, Aug 17, 2009 at 5:27 PM, Sean Zhang<seanecon at gmail.com> wrote:
> Dear R helpers:
>
> I wonder how to pass more than one argument to the function called by
> lapply.
> For example,
>
> #R code below ---------------------------
>
> indf <- data.frame(id=I(c('a','b')),y=c(1,10))
> #I want to add an addition argument cutoff into the function called by
> lapply.
> outside.fun <- function(indf, cutoff)
> {
> unlist(lapply(split(indf, indf[,'id']), function(.x, cutoff) {.x[,'y'] <
> cutoff} ))
> }
> #but the next line does not work
> outside.fun(indf,3)
>
> #as you expected, hard code cutoff works as below, but I do not like hard
> coding.
> outside.fun.hardcode.cutoff <- function(indf, cutoff)
> {
> unlist(lapply(split(indf, indf[,'id']), function(.x, cutoff) {.x[,'y'] < 3}
> ))
> }
> outside.fun.hardcode.cutoff(indf,)
>
> #R code above----------------------------
>
> So, can someone kindly show me how to pass more than one arguments into the
> function called by lapply?
>
> Many thanks in advance.
>
> -Sean
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
--
Jim Holtman
Cincinnati, OH
+1 513 646 9390
What is the problem that you are trying to solve?
More information about the R-help
mailing list