I have daily data over several years for several currencies. I would like to lag variables in the data set by exactly one month (i.e. June 15 to July 15, not necessarily 30 days). NA's are fine where that is not possible.
I have gotten by so far by writing something like this:
ddply(Data, .(Currency), function(x){ #First column is date, 2nd is Currency, rest data.
y=x[,-(3)] #this is all data I want lagged
y$date=as.Date(y$date) %m+% months(1) #This increases the dates by one month
x$date=as.Date(x$date) #what data I dont want lagged is x[, (1:3)] below
z=merge(x[,(1:3)], y, by=c("date", "Currency")) #merge by date,
#lagged stuff merges with non-lagged stuff 1 month later than original obs date
return(z)
})
I can include the data if necessary, but given that I have something that works already I dont want anyone to spend time on it.
I just want to check that I cant use the lubridate %m+% months(1) syntax within the lag function. I have tried the lag function from package "statar" that uses the along_by syntax but haven't been able to figure it out.
Thanks!