【问题标题】:making time series regular使时间序列有规律
【发布时间】:2012-07-06 06:46:46
【问题描述】:

我这个时间序列数据(我不能dput。因为dput给出了很长的结果,在这里复制粘贴会很乱) 这些是我的文本文件中的几行:

        Time                  Depths   ifc  if   lat        lon
"7814" "2012-03-15 04:45:09-05" 4816 1040 5410 43.213628 -92.27727
"7815" "2012-03-15 04:30:04-05" 4813 1040 5410 43.213628 -92.27727
"7816" "2012-03-15 04:15:14-05" 4807 1040 5410 43.213628 -92.27727
"7817" "2012-03-15 04:00:09-05" 4809 1040 5410 43.213628 -92.27727
"7818" "2012-03-15 03:45:04-05" 4819 1040 5410 43.213628 -92.27727
"7819" "2012-03-15 03:30:15-05" 4816 1040 5410 43.213628 -92.27727
"7820" "2012-02-25 14:45:07-06" 4862 1040 5410 43.213628 -92.27727
"7821" "2012-02-25 14:30:02-06" 4858 1040 5410 43.213628 -92.27727
"7822" "2012-02-25 14:15:13-06" 4852 1040 5410 43.213628 -92.27727
"7823" "2012-02-25 14:00:08-06" 4860 1040 5410 43.213628 -92.27727
"7824" "2012-02-25 13:45:03-06" 4855 1040 5410 43.213628 -92.27727
"7825" "2012-02-25 13:30:13-06" 4869 1040 5410 43.213628 -92.27727
"7826" "2012-02-25 13:15:08-06" 4868 1040 5410 43.213628 -92.27727
"7827" "2012-02-25 13:00:03-06" 4873 1040 5410 43.213628 -92.27727

在这里您可以看到第 1 行旁边的值。 7819是一个跳跃。我希望解决此问题,使其包含 15 分钟的连续时间间隔,并且这些间隔中的深度列为 NA,其余列中的值将填充为其他行中的常量值。

我尝试了this on SO,但没有成功。 有人可以帮我解决这个问题吗?

【问题讨论】:

  • -05 和 -06 这是时区的表示法。您可以通过以下方式忽略它们:将这些行保存在文本文件中,然后使用 read.zoo("dat.txt", tz="") 读取它
  • 我已经编辑了我的问题,现在根据您的建议包含了 dput。
  • 因为这已经是zoo 对象,请尝试使用dput(t[7814:7827])
  • 这是一个常见问题。请参阅动物园常见问题解答 #13:cran.r-project.org/web/packages/zoo/vignettes/zoo-faq.pdf

标签: r time-series zoo


【解决方案1】:

如果我正确理解了您的问题,应该这样做(假设它不是zoo 对象,data.framet)。您可能还必须将列索引 2 更改为 1

dates <- as.POSIXct(t[,2])
# remove the seconds from the time stamps
dates <- dates - as.numeric(format(dates,"%S"))
# Create a sequence of dates for the entire time.
all_dates <- seq.POSIXt(from=dates[length(dates)], to=dates[1],by="15 min")
# Put them into a data.frame to make merging easier
all_dates_frame <- data.frame(dates_floor=all_dates)
# create a data.frame of the obsereved values with the floored dates
t_floor <- data.frame(dates_floor=dates, t[,-2]) 
# merge the observations onto the grid
together <- merge(all_dates_frame, t_floor, all.y="TRUE", all.x="TRUE")
# If you want to replace the floored times with the actual times then find which ones need replacing
replace_time_index <- !is.na(together[,2])
# replace the time stamps
together[replace_time_index, 1] <- t[,2] 

【讨论】:

    【解决方案2】:

    @Jase_ 提供了一个示例,说明如果您的数据是 data.frame,您将如何执行此操作;但是,您在 cmets 和您最初尝试使用 dput 时指出,这是一个带有 zoo 类的对象。

    这是zoo 解决方案(从概念上借用 Jase_ 的答案)。它利用zoo 对象的index 属性。

    # First, read in your data as a zoo object via "copy and paste"
    require(zoo)
    t = read.zoo(text='        Time Depths   ifc  if   lat        lon
    "7814" "2012-03-15 04:45:09-05" 4816 1040 5410 43.213628 -92.27727
    "7815" "2012-03-15 04:30:04-05" 4813 1040 5410 43.213628 -92.27727
    "7816" "2012-03-15 04:15:14-05" 4807 1040 5410 43.213628 -92.27727
    "7817" "2012-03-15 04:00:09-05" 4809 1040 5410 43.213628 -92.27727
    "7818" "2012-03-15 03:45:04-05" 4819 1040 5410 43.213628 -92.27727
    "7819" "2012-03-15 03:30:15-05" 4816 1040 5410 43.213628 -92.27727
    "7820" "2012-02-25 14:45:07-06" 4862 1040 5410 43.213628 -92.27727
    "7821" "2012-02-25 14:30:02-06" 4858 1040 5410 43.213628 -92.27727
    "7822" "2012-02-25 14:15:13-06" 4852 1040 5410 43.213628 -92.27727
    "7823" "2012-02-25 14:00:08-06" 4860 1040 5410 43.213628 -92.27727
    "7824" "2012-02-25 13:45:03-06" 4855 1040 5410 43.213628 -92.27727
    "7825" "2012-02-25 13:30:13-06" 4869 1040 5410 43.213628 -92.27727
    "7826" "2012-02-25 13:15:08-06" 4868 1040 5410 43.213628 -92.27727
    "7827" "2012-02-25 13:00:03-06" 4873 1040 5410 43.213628 -92.27727
    ', tz="")
    

    合并后的输出只有两行:

    # Modify your index as per Jase_'s answer
    index(t) = index(t) - as.numeric(format(index(t), "%S"))
    # Merge with an empty zoo object that has an index
    # of all the dates that you need.
    t.merged = merge(t, zoo(, seq(from=index(t)[1], 
                                  to=index(t)[length(index(t))], 
                                  by="15 min")))
    

    让我们看看输出是什么样子的:

    head(t.merged, 10L)
    # 
    # 2012-02-25 13:00:00 4873 1040 5410 43.21363 -92.27727
    # 2012-02-25 13:15:00 4868 1040 5410 43.21363 -92.27727
    # 2012-02-25 13:30:00 4869 1040 5410 43.21363 -92.27727
    # 2012-02-25 13:45:00 4855 1040 5410 43.21363 -92.27727
    # 2012-02-25 14:00:00 4860 1040 5410 43.21363 -92.27727
    # 2012-02-25 14:15:00 4852 1040 5410 43.21363 -92.27727
    # 2012-02-25 14:30:00 4858 1040 5410 43.21363 -92.27727
    # 2012-02-25 14:45:00 4862 1040 5410 43.21363 -92.27727
    # 2012-02-25 15:00:00   NA   NA   NA       NA        NA
    # 2012-02-25 15:15:00   NA   NA   NA       NA        NA
    tail(t.merged, 10L)
    # 
    # 2012-03-15 02:30:00   NA   NA   NA       NA        NA
    # 2012-03-15 02:45:00   NA   NA   NA       NA        NA
    # 2012-03-15 03:00:00   NA   NA   NA       NA        NA
    # 2012-03-15 03:15:00   NA   NA   NA       NA        NA
    # 2012-03-15 03:30:00 4816 1040 5410 43.21363 -92.27727
    # 2012-03-15 03:45:00 4819 1040 5410 43.21363 -92.27727
    # 2012-03-15 04:00:00 4809 1040 5410 43.21363 -92.27727
    # 2012-03-15 04:15:00 4807 1040 5410 43.21363 -92.27727
    # 2012-03-15 04:30:00 4813 1040 5410 43.21363 -92.27727
    # 2012-03-15 04:45:00 4816 1040 5410 43.21363 -92.27727
    

    但是,这不会像您想要的那样替换 NA 值。

    【讨论】:

      猜你喜欢
      • 2023-04-04
      • 2013-01-09
      • 2011-04-23
      • 2016-04-07
      • 2014-09-02
      • 1970-01-01
      • 2017-01-29
      • 2012-05-12
      • 2021-01-08
      相关资源
      最近更新 更多