I have an xts object (indexed by Dates)
start <- as.Date('2016-06-30')
end <- as.Date('2016-12-31')
dt_len <- as.integer(end - start)
x <- matrix(rep(NA, 8 * dt_len), ncol = 8)
colnames(x) <- c("baseline", "cumul_baseline",
"scenario_1", "cumul_scenario_1",
"scenario_2", "cumul_scenario_2",
"scenario_3", "cumul_scenario_3")
dt_ts <- xts(x, start + 1:as.integer(dt_len))
Into which I insert a baseline measure of sales. The purpose is to build out 3 different scenarios of sales and compare the actual sales to those scenarios, IOT know "which" of these scenarios we think we're "in."
# baseline sales (constant)
b_daily <- 1561
dt_ts$baseline <- b_daily
I then make some "actual" (hypothetical) sales data over the same date range.
eg_data <- data.frame(Date = seq.Date(from = as.Date("2016-07-01"), by = "day", length.out = dt_len), Sales = sample(1000:2000, dt_len))
But if I am trying to join my hypothetical eg_data
object to the xts object with my scenarios...
left_join(dt_ts, eg_data, by = "Date")
Error in UseMethod("left_join") :
no applicable method for 'left_join' applied to an object of class "c('xts', 'zoo')"
My question is: if I have a data.frame I want to left_join to an object I'd like to keep cast as an xts
, how to I join them? I'd like to assume that the row-dates don't line up necessarily, thus requiring a call to join
or merge
...
答案 0 :(得分:1)
The simple answer to your question is "don't". Either convert them both to xts or data.frame.
Here's how to do it with both objects as xts, since storing time series in a data.frame makes me feel icky.
xts_data <- merge(dt_ts, Sales=with(eg_data, xts(Sales, Date)), join="left")