【问题标题】:Populating missing Date and Time in time-series data in R, with zoo package使用 zoo 包在 R 中的时间序列数据中填充缺失的日期和时间
【发布时间】:2018-09-02 19:08:14
【问题描述】:

我有四分之一小时(15 分钟间隔)的频率数据。

sasan<-read.csv("sasanhz.csv", header = TRUE)

head(sasan)
               Timestamp Avg.Hz
1 12/27/2017 12:15:00 AM  50.05
2 12/27/2017 12:30:00 AM  49.99
3 12/27/2017 12:45:00 AM  49.98
4 12/27/2017 01:00:00 AM  50.01
5 12/27/2017 01:15:00 AM  49.97
6 12/27/2017 01:30:00 AM  49.98

str(sasan)
'data.frame':   5501 obs. of  2 variables:
 $ Timestamp: Factor w/ 5501 levels "01/01/2018 00:00:00 AM",..: 5112 5114 5116 5023 5025 
                                 5027 5029 5031 5033 5035 ...
 $ Avg.Hz   : num  50 50 50 50 50 ...

 #change to posixct

sasan$Timestamp<-as.POSIXct(sasan$Timestamp, format="%m/%d/%Y %I:%M:%S %p")

在这个时间序列中,我在列“时间戳”中有一些缺失的数据时间,我想估算缺失的日期时间。 我试过zoo

    z<-zoo(sasan)
    > head(z[1489:1497])
     Timestamp           Avg.Hz
1489 2018-01-11 12:15:00 50.02 
1490 2018-01-11 12:30:00 49.99 
1491 2018-01-11 12:45:00 49.94 
1492 <NA>                49.98 
1493 <NA>                50.02 
1494 <NA>                49.95

zoo 包中使用“na.locf”函数输入日期和时间的 NA 值时,出现以下错误。

 sasan_mis<-seq(start(z), end(z), by = times("00:15:00"))
> na.locf(z, xout = sasan_mis)
Error in approx(x[!na], y[!na], xout, ...) : zero non-NA points
In addition: Warning message:
In xy.coords(x, y, setLab = FALSE) : NAs introduced by coercion

如何克服这个错误?我如何估算这个丢失的日期时间?感谢您的建议。

dput(head(z))
structure(c("2017-12-27 00:15:00", "2017-12-27 00:30:00", "2017-12-27 00:45:00", 
"2017-12-27 01:00:00", "2017-12-27 01:15:00", "2017-12-27 01:30:00", 
"50.05", "49.99", "49.98", "50.01", "49.97", "49.98"), .Dim = c(6L, 
2L), .Dimnames = list(NULL, c("Timestamp", "Avg.Hz")), index = 1:6, class = "zoo")

我用过的库包是

library(ggplot2)
library(forecast)
library(tseries)
library(xts)
library(zoo)
library(dplyr)

【问题讨论】:

  • zoo 没有大写 Z。而且我认为错误不是来自 na.locf 调用,而是seq 调用对象的结果由start 重播。您从哪里获得 startstoptimes 函数?需要完整的代码(包括加载所有需要的包的代码)。从一个干净的会话开始,然后显示已完成的操作以及用于调试错误的 sessionInfo() 输出。
  • 发布dput(head(z)) 的输出。我猜startstop 无法使用具有非数据时间索引值的动物园对象存在问题。你的动物园对象有整数索引。
  • 您需要在将它读入动物园之前修复它。完成后,您可以使用read.zoo(my_data_frame)zoo(data, index) 将其读入动物园。

标签: r time-series zoo


【解决方案1】:

假设 OP 在数据中缺少 Timestamp 变量值并寻找填充它的方法。

zoo 包中的na.approx 在这种情况下非常方便。

# na.approx from zoo to populate missing values of Timestamp
sasan$Timestamp <- as.POSIXct(na.approx(sasan$Timestamp), origin = "1970-1-1")
sasan
# 1  2017-12-27 00:15:00  50.05
# 2  2017-12-27 00:30:00  49.99
# 3  2017-12-27 00:45:00  49.98
# 4  2017-12-27 01:00:00  50.01
# 5  2017-12-27 01:15:00  49.97
# 6  2017-12-27 01:30:00  49.98
# 7  2017-12-27 01:45:00  49.98
# 8  2017-12-27 02:00:00  50.02
# 9  2017-12-27 02:15:00  49.95
# 10 2017-12-27 02:30:00  49.98

数据

# OP's data has been slightly modified to include NAs
sasan <- read.table(text = 
"Timestamp           Avg.Hz
1 '12/27/2017 12:15:00 AM'  50.05
2 '12/27/2017 12:30:00 AM'  49.99
3 '12/27/2017 12:45:00 AM'  49.98
4 '12/27/2017 01:00:00 AM'  50.01
5 '12/27/2017 01:15:00 AM'  49.97
6 '12/27/2017 01:30:00 AM'  49.98
7 <NA>                      49.98 
8 <NA>                      50.02 
9 <NA>                      49.95
10 '12/27/2017 02:30:00 AM'  49.98", 
header = TRUE, stringsAsFactors = FALSE)

# convert to POSIXct 
sasan$Timestamp<-as.POSIXct(sasan$Timestamp, format="%m/%d/%Y %I:%M:%S %p")

【讨论】:

  • @AvijitMallick 看看答案。如果不符合您的要求,请告诉我。此外,您可以通过单击答案框左侧的tick 符号来接受答案,以便对未来的用户有所帮助。你可以看看stackoverflow.com/help/someone-answers
  • 感谢您的支持。
猜你喜欢
  • 2011-04-03
  • 2016-12-08
  • 2019-05-16
  • 2015-12-03
  • 1970-01-01
  • 1970-01-01
  • 2021-07-01
  • 2021-06-01
  • 1970-01-01
相关资源
最近更新 更多