r - Can I lag daily data by months with lag ( )? -
i have daily data on several years several currencies. lag variables in data set 1 month (i.e. june 15 july 15, not 30 days). na's fine not possible.
i have gotten far writing this:
ddply(data, .(currency), function(x){ #first column date, 2nd currency, rest data. y=x[,-(3)] #this data want lagged y$date=as.date(y$date) %m+% months(1) #this increases dates 1 month x$date=as.date(x$date) #what data dont want lagged x[, (1:3)] below z=merge(x[,(1:3)], y, by=c("date", "currency")) #merge date, #lagged stuff merges non-lagged stuff 1 month later original obs date return(z) })
i can include data if necessary, given have works dont want spend time on it.
i want check cant use lubridate %m+% months(1) syntax within lag function. have tried lag function package "statar" uses along_by syntax haven't been able figure out.
thanks!
Comments
Post a Comment