1.这是否总是工作(我会用这里面的功能)
是。
2.如何在相应的开始前添加句点?
下面是在开始和结束边界上展开索引的示例,默认情况下使用NA
值进行填充。只要数据的列匹配,您也可以使用rbind(xts1, xts2)
在开始或结束时轻松添加实际的xts数据。
# add dateFormat = "Date" to avoid misaligned timestamps
# in relation to time component HH:MM:SS when `rbind`ing with
# Date type indexes (as opposed to POSIXct type indexes):
xts_object <- as.xts(sample_matrix, descr='my new xts object', dateFormat="Date")
my_periodicity <- unclass(periodicity(xts_object))$label
add_right <- 5
v_dates_end <- seq(as.Date(end(xts_object)),
by = my_periodicity,
length.out = (add_right+1))[-1]
st_date <- as.Date(start(xts_object))
v_dates_start <- seq(from = st_date - 5,
to = st_date - 1,
by = my_periodicity)
x_expand_time_index <- xts(, order.by = c(v_dates_start, v_dates_end))
# Note the fill argument could be replaced by say 0, 1, NaN, etc
x <- merge(xts_object, x_expand_time_index, fill = NA)
head(x)
# Open High Low Close
# 2006-12-28 NA NA NA NA
# 2006-12-29 NA NA NA NA
# 2006-12-30 NA NA NA NA
# 2006-12-31 NA NA NA NA
# 2007-01-01 NA NA NA NA
# 2007-01-02 50.03978 50.11778 49.95041 50.11778
tail(x)
# Open High Low Close
# 2007-06-30 47.67468 47.94127 47.67468 47.76719
# 2007-07-01 NA NA NA NA
# 2007-07-02 NA NA NA NA
# 2007-07-03 NA NA NA NA
# 2007-07-04 NA NA NA NA
# 2007-07-05 NA NA NA NA
非常感谢约书亚! – 4554888