Home > OS >  Join a xts object and a data frame by date, one day forward, in R
Join a xts object and a data frame by date, one day forward, in R

Time:07-04

I have the following xts object and data frame in R.

xts:

enter image description here

data frame:

enter image description here

I need to join them by date, the date in the xts object being one workday forward the date in the data frame, for each record that needs to be matched.

So I would need to find what is the closest day forward in the xts object so to join the data from the data frame.

Desired result (xts object):

enter image description here

CodePudding user response:

The way I solved this was to lag the 'date' column from the xts object, then added it as a new column to a data frame object. After that I joined the data frames as usual.

lag_dates <- lag(index(returns))
returns <- cbind(data.frame(returns), lag_dates = as.Date(lag_dates))
  • Related