简体   繁体   中英

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

Let's consider a simultaneous data collection:

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

I can create a time serie object using:

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

What if I want to have the time serie to describe the two variables a and b, like

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

The result seem to merge both data:

                [,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

Is there a way to split the two variables in the same time serie?

Indeed there is a way:

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

The first argument is

an object containing the time series data

meaning that it may be multivariate, as in your case, while the second argument is

a corresponding vector of unique times/dates

so there is no need to provide a matrix of times/dates.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM