I'm trying to write a function using ddply package. I've read several solutions, but I couldn't figure it out how to tackle this issue in my case.
Rolling <- function(ID, RP){
require(plyr)
cn=colnames(ID)
Rolling3M=ddply(ID, cn[2:(length(cn)-1)], transform, Rolling3M = as.numeric(filter(v3,c(1,rep(1,times=RP)),sides=1)))
return(Rolling3M)
}
v1=c("a","a","a","a","a","a","a","b","b","b","b","b","b")
v2=c("c","c","c","d","d","d","d","c","c","c","d","d","d")
v3=c(1:13)
df = data.frame(v1,v2,v3)
output=Rolling(df, 3)
I got this error message: Error: object 'RP' not found
I'd like to do the same as this would do:
output=ddply(df, .(v1,v2),transform, Rolling3M=as.numeric(filter(v3,c(1,rep(1,2)), sides=1)))