0

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!

  • What do you plan to do with January 31? Feb 28, NA, March 3? – Gregor Thomas Apr 01 '15 at 16:18
  • Throw them out. At a later step I do something like: `SubReturns <- Returns[as.numeric(format(time(Returns), "%d")) >= 15] SubReturns=SubReturns[!duplicated(as.yearmon(time(SubReturns)))]` – Evan Smith Apr 01 '15 at 17:08
  • I just want to be able to have multiple return paths, but I dont need all 30. It just matters that the middle dates do it correctly. – Evan Smith Apr 01 '15 at 17:10
  • why not use endpoints? or extract the indexes you care about, build your streams, and then cbind them back together for charting, etc. ? – Brian G. Peterson Apr 11 '15 at 11:15
  • This might be helpful: http://stackoverflow.com/questions/14169620/add-a-month-to-a-date – chandler Apr 18 '15 at 09:56
  • Thanks for the help all, I was looking specifically for something to pass into lag, but that doesn't seem possible which is fine. – Evan Smith May 05 '15 at 20:55

0 Answers0