我对如何简洁地做到这一点感到有些困惑。我的解决方案使用 matrix(...) 工作,但似乎有点过于生硬。是否有更好的方法?
我有一个现有的 1 行 xts 对象(D1),以及来自外部来源的非 xts 向量(E1),我想将它们合并在一起:
library(quantmod)
getSymbols("SPY")
data = get("SPY")
is.xts(data)
# Extract a single row xts object
D1 = data[1,]
index(D1)
# Get the numerical part as a vector
D1v = as.vector(D1)
# New data from external source to be merged
E1 = c(5,6,7,8,9,10)
# Combine into new vector
G1 = c(D1v, E1)
G1
# This fails, I think because xts treats G1
# like a 1-column object, not a 1-row object?
testXts1 = xts(G1, order.by = index(D1))
testXts2 = xts(matrix(G1, nrow=1), order.by = index(D1))
index(testXts2)
testXts2
我的问题是 matrix(G1, nrow=1) 是否是这种操作的预期方法,问题在于xts要求X和index(X)具有相同的维度。
谢谢!
xts(t(G1), index(D1))
- GSee