Richard Ma
2011-Aug-09 03:01 UTC
[R] How to pass different arguments to a function within lapply()?
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.
David Winsemius
2011-Aug-09 03:57 UTC
[R] How to pass different arguments to a function within lapply()?
On Aug 8, 2011, at 11:01 PM, Richard Ma wrote:> 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) > --------------------------------------------------------------------------> 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