xts 可以处理多个具有相同时间索引的时间序列吗?

Can xts deal with several time series with identical time indices?

让我们考虑同时收集数据:

library(xts)
d <- data.frame(t = Sys.time()-1:10, a = 1:10, b = 11:20 )

我可以使用以下方法创建时间序列对象:

df.xts <- xts(d$a, order.by=d$t)

如果我想用时间序列来描述两个变量a和b怎么办,比如

df.xts <- xts(c(d$a,d$b), order.by=c(d$t,d$t))

结果似乎合并了两个数据:

                [,1]
2018-02-16 15:13:19   10
2018-02-16 15:13:19   20
2018-02-16 15:13:20    9
2018-02-16 15:13:20   19
2018-02-16 15:13:21    8

有没有办法在同一时间序列中拆分两个变量?

确实有办法:

df.xts <- xts(cbind(d$a, d$b), order.by = d$t)

第一个参数是

an object containing the time series data

意味着它可能是多变量的,就像你的情况一样,而第二个参数是

a corresponding vector of unique times/dates

所以不需要提供times/dates的矩阵。