简体   繁体   中英

combine two zoo time series

I have 2 sucesive ZOO time series (the date of one begins after the other finishes), they have the following form (but much longer and not only NA values):

a:

1979-01-01 1979-01-02 1979-01-03 1979-01-04 1979-01-05 1979-01-06 1979-01-07 1979-01-08 1979-01-09 
    NA         NA         NA         NA         NA         NA         NA         NA         NA 

b:

1988-08-15 1988-08-16 1988-08-17 1988-08-18 1988-08-19 1988-08-20 1988-08-21 1988-08-22 1988-08-23 1988-08-24 1988-08-25 
    NA         NA         NA         NA         NA         NA         NA         NA         NA         NA         NA 

all I want to do is combine them in one time serie as a ZOO object, it seems to be a basic task but I am doing something wrong. I use the function "merge":

combined <- merge(a, b)

but the result is something in the form:

             a   b
1980-03-10  NA   NA
1980-03-11  NA   NA
1980-03-12  NA   NA
1980-03-13  NA   NA
1980-03-14  NA   NA
1980-03-15  NA   NA
1980-03-16  NA   NA
.
.

which is not a time series, and the lengths dont fit:

> length(a)
[1] 10957
> length(b)
[1] 2557
> length(combined)
[1] 27028

how can I just combine them into one time series with the form of the original ones?

Assuming the series shown reproducibly in the Note at the end, the result of merging the two series has 20 times and 2 columns (one for each series). The individual series have lengths 9 and 11 elements and the merged series is a zoo object with 9 + 11 = 20 rows (since there are no intersecting times) and 2 columns (one for each input) and length 40 (= 20 * 2). Note that the length of a multivariate series is the number of elements in it, not the number of time points.

length(z1)
## [1] 9
length(z2)
## [1] 11

m <- merge(z1, z2)

class(m)
## [1] "zoo"

dim(m)
## [1] 20  2

nrow(m)
## [1] 20

length(index(m))
## [1] 20

length(m)
## [1] 40

If what you wanted is to string them out one after another then use c :

length(c(z1, z2))
## [1] 20

The above are consistent with how merge , c and length work in base R.

Note:

library(zoo)

z1 <- zoo(rep(NA, 9), as.Date(c("1979-01-01", "1979-01-02", "1979-01-03", 
"1979-01-04", "1979-01-05", "1979-01-06", "1979-01-07", "1979-01-08", 
 "1979-01-09")))

z2 <- zoo(rep(NA, 11), as.Date(c("1988-08-15", "1988-08-16", "1988-08-17",
"1988-08-18", "1988-08-19", "1988-08-20", "1988-08-21", "1988-08-22", 
"1988-08-23", "1988-08-24", "1988-08-25")))

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