Re: [R] lapply with functions with changing parameters

From: Erik Iverson <>
Date: Tue, 01 Jun 2010 16:00:23 -0500

Bunny, wrote:
> Dear all,
> I am trying to avoid a for loop here and wonder if the following is
> possible:
> I have a data.frame with 6 columns and i want to get a
> cross-correlogram (by using ccf) . Obivously ccf only accepts two
> columns at once and then returms a list. In fact, with a for loop iŽd
> do the following
> for (i in 1:6) {
> x[[i]]=ccf(mydf[,i],mydf[,6])
> }
> Is there any chance to the same with lapply? e.g. lapply(mydf,"ccf",
> .... ) with ... respresenting the changing arguments for ccf
> functions (note only the first argument does actually change)

You don't give a reproducible example, but since you want to apply a function to a list, the answer is yes. Just defining the function and the list is the trick, untested:

lapply(mydf[, 1:5], function(x) ccf(x, mydf[, 6])) mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code. Received on Tue 01 Jun 2010 - 21:06:18 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 01 Jun 2010 - 21:50:26 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive