[R] How to pass different arguments to a function within lapply()?

Richard Ma xuanlong.ma at uts.edu.au
Tue Aug 9 05:01:07 CEST 2011


Hi all,

I have a data frame called "rst", see below:
------------------------------------------------------------------------------------------
# This is a paste able example

# In case you don't have "KernSmooth" package installed, please uncomment
below line.
# install.packages("KernSmooth")

library(KernSmooth)

rst <- data.frame(hsp = rnorm(23), dal = rnorm(23)

gs <- 350
ts <- seq(1, 353, 16)
                  
bw <- lapply(rst, dpill, x = ts, gridsize = gs)

# If I just want to apply locpoly function seperately
lp.hsp <- locpoly(x = ts, y = rst$hsp, bandwidth = bw$hsp, gridsize = gs)
lp.dal <- locpoly(x = ts, y = rst$dal, bandwidth = bw$dal, gridsize = gs)
--------------------------------------------------------------------------------------------

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.



More information about the R-help mailing list