将多个不规则时间序列转换为常规时间序列

时间:2014-07-11 14:14:43

标签: r dataframe time-series

我有一个多个不规则时间序列(data.frame)的data.frame,看起来像这样

station   Time     WaterTemp
1       01-01-1974  5.0000000
1       01-02-1974  5.0000000
1       01-03-1974  8.6000004
1       01-05-1974  8.1333332
1       01-07-1974  12.7999999
2       01-01-1974  5.0000000
2       01-02-1974  5.0000000
2       01-04-1974  8.6000004
2       01-06-1974  8.1333332
2       01-08-1974  12.7999999

我想将其转换为常规时间序列(ts)对象,该对象应该如下所示

Time        Staion1     Station2
 Jan1974    5.0000000  5.0000000
 Feb1974    5.0000000  5.0000000
 Mar1974    8.6000004  NA
 Apr1974    NA         8.6000004
 May1974    8.1333332  NA
 June1974   NA         8.1333332
 July1974   12.7999999 NA
  Aug1974  NA         12.7999999
  Sep1974  NA         NA
  Oct1974  7.9         NA
  Nov1974  NA         NA
  Dec1974  NA         7.4

我该怎么做? 虽然单个时间序列有很多解决方案,但我没有遇到过多个时间序列的处理。

谢谢,

1 个答案:

答案 0 :(得分:1)

如果DF是您的数据框,请尝试此操作。在最后一行转换为ts使其成为常规,然后我们转换回动物园:

library(zoo)
z <- read.zoo(DF, split = 1, index = 2, format = "%d-%m-%Y")
z.ym <- aggregate(z, as.yearmon, identity) # convert to yearmon
zm <- aggregate(as.zoo(as.ts(z.ym)), as.yearmon, identity)

最后一行的替代方案是这两行:

g <- zoo(, seq(start(z.ym), end(z.ym), deltat(z.ym))) # grid
zm <- merge(z.ym, g)

在任何一种情况下,此时coredata(zm)是数据部分而time(zm)是索引,尽管您可能希望将其保留为动物园对象,以便您可以使用其他时间序列设施和许多其他包接受该表格的时间序列。

注意:以下是一个完整的自包含可重复示例:

DF <- structure(list(station = c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 
2L), Time = structure(c(1L, 2L, 3L, 5L, 7L, 1L, 2L, 4L, 6L, 8L
), .Label = c("01-01-1974", "01-02-1974", "01-03-1974", "01-04-1974", 
"01-05-1974", "01-06-1974", "01-07-1974", "01-08-1974"), class = "factor"), 
    WaterTemp = c(5, 5, 8.6000004, 8.1333332, 12.7999999, 5, 
    5, 8.6000004, 8.1333332, 12.7999999)), .Names = c("station", 
"Time", "WaterTemp"), class = "data.frame", row.names = c(NA, 
-10L))

library(zoo)
z <- read.zoo(DF, split = 1, index = 2, format = "%d-%m-%Y")
z.ym <- aggregate(z, as.yearmon, identity) # convert to yearmon
zm <- aggregate(as.zoo(as.ts(z.ym)), as.yearmon, identity)

,并提供:

> zm
                 1         2
Jan 1974  5.000000  5.000000
Feb 1974  5.000000  5.000000
Mar 1974  8.600000        NA
Apr 1974        NA  8.600000
May 1974  8.133333        NA
Jun 1974        NA  8.133333
Jul 1974 12.800000        NA
Aug 1974        NA 12.800000

已更新一些更正和改进。