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@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.

Reply via email to