Q:

R: Looping a stats::filter through ddply

I'm attempting to run a series of different filters across some data, by a grouping variable. As an example, this code works to produce one filter (0.1) across a grouping variable (gear).
library(plyr)
library(BBmisc)
mtcars <- mtcars[order(mtcars$gear),]
mtcars2 <- ddply(mtcars,.(gear),transform,mpgfilter = as.numeric(stats::filter(mpg, filter=0.1, method="recursive")))

But I want a method so I can feed in multiple filters. I was thinking of just nesting the code within a lapply function. As an example, here are the varying filters (as a list):
newdata <- as.data.frame(seq(from = 0.1, to = 0.9, by = 0.1))
newdata <- as.data.frame(t(newdata))
colnames(newdata) = newdata[1, ]
colnames(newdata) <- paste("V", colnames(newdata), sep = "_")
newlist <- convertColsToList(newdata)

And here is what I was thinking for a solution (not working):
newlist3 <- lapply(newlist, function(y){
  mtcars2 <- ddply(mtcars,.(gear), transform, mpgfilter = as.numeric(stats::filter(mpg, filter=y, method="recursive")))
})

Any suggestions? My final output would be a dataframe with each column showing the output from the different filters. Answer doesn't have to make use of lapply/ddply if there is a better way. 
Thanks.

A:

I think I've answered my own question in a roundabout fashion, using the 'by' function instead of ddply:
new <- lapply(newlist, function(y){
by(mtcars, mtcars$gear, function(x) mpgfilter = as.numeric(stats::filter(x$mpg, filter=y, method="recursive")))})

new2 <- lapply(new, function(y){
  resultsdf <- as.data.frame(t(do.call(rbind,y)))})
df <- as.data.frame(ldply(new2, data.frame))
df2 <- melt(df)
df2 <- ddply(df2,".id",transform,ID=1:length(.id))
finaldata <- dcast(df2, ID + variable ~ .id, value.var = "value")

Bit messy with the extra work required to get it back to a workable dataframe, but does the trick.

