[R] How to pass different arguments to a function within lapply()?
David Winsemius
dwinsemius at comcast.net
Tue Aug 9 14:34:06 CEST 2011
On Aug 9, 2011, at 2:10 AM, Richard Ma wrote:
> Hi David,
>
> Thank you very much. Problem solved.
>
> sapply() and lapply() are so powerful that even I have read their
> documents
> several times, still lots of tricky stuff to learn and try.
The basic difference between my approach and yours was that after
noticing that you needed to iterate over the indices of two different
structures, I used them as the arguments. This would also have worked
and be generalizable in situations where the two lists shared column
names or named indices:
sapply( names(rst), function(x) locpoly(x=ts, y=rst[[x]],
bandwidth=bw[[x]], gridsize=gs) )
I also considered code that used mapply and the general form might
have been:
mapply(locpoly, y=rst, bandwidth=bw, MoreArgs=list(x=ts, gridsize=gs) )
Seems also to succeed, and is more along the logic htat you were
attempting.
--
David.
>
> Thank you again!
> Richard
>
>
> David Winsemius wrote:
>>
>>
>>> sapply(c("hsp", "dal"), function(x) locpoly(x=ts, y=rst[[x]],
>> bandwidth=bw[[x]], gridsize=gs) )
>> hsp dal
>> x Numeric,350 Numeric,350
>> y Numeric,350 Numeric,350
>>
>>
>> --
>> David.
>>>
>>> In above example, I can apply the "locpoly()" to each column of
>>> “rst”. But
>>> if I want to apply this function to all columns of "rst", I guess I
>>> should
>>> use lapply() function.
>>>
>>> However, the "bandwidth", one of the locpoly()'s arguments should be
>>> different due to different columns of "rst". It means that when R
>>> apply
>>> locpoly() to first column "hsp", the "bandwidth" should be set as
>>> correspondent bandwidth.
>>>
>>> My problem is, how to pass the "bandwidth" argument to "locpoly"
>>> within
>>> lapply() in terms of different column names?
>>>
>>> My example just tedious if the "rst" has lots of columns. So I
>>> wonder if
>>> there exist more convenient way to get this job done.
>>>
>>> Thank you.
>>>
>>> -----
>>> Richard Ma
>>> PhD student, Ecology & Remote Sensing
>>> Climate Change Cluster, Department of Environment Science
>>> University of Technology, Sydney
>>> --
>>> View this message in context:
>>> http://r.789695.n4.nabble.com/How-to-pass-different-arguments-to-a-function-within-lapply-tp3728858p3728858.html
>>> Sent from the R help mailing list archive at Nabble.com.
>>>
>>> ______________________________________________
>>> 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.
>>
>> David Winsemius, MD
>> West Hartford, CT
>>
>> ______________________________________________
>> 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.
>>
>
>
> -----
> Richard Ma
> PhD student, Ecology & Remote Sensing
> Climate Change Cluster, Department of Environment Science
> University of Technology, Sydney
> --
> View this message in context: http://r.789695.n4.nabble.com/How-to-pass-different-arguments-to-a-function-within-lapply-tp3728858p3729070.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.
David Winsemius, MD
West Hartford, CT
More information about the R-help
mailing list