且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何添加缺少的日期并删除每小时时间序列中的重复日期

更新时间:2023-02-26 19:19:23

zoo FAQ 小插图 解决了关于填充时间序列的部分.read.zoo 中的 aggregate 参数处理重复项.在这种情况下,我们对它们求平均值,但我们可以采取其他操作,例如使用 FUN = function(x) tail(x, 1).我们在这里使用 chron 日期/时间来避免时区问题(参见 R News 4/1)但我们可以使用 POSIXct如果时区相关 - 它们似乎不相关,因为它们不在输入中.

FAQ #13 in the the zoo FAQ vignette addresses the part about filling time series. The aggregate argument in read.zoo handles the duplicates. In this case we average them but we could have taken other action such as using FUN = function(x) tail(x, 1). We use chron date/times here to avoid time zone problems (see R News 4/1) but we could have used POSIXct if time zones were relevant -- they seem not since they are not in the input.

Lines <- "date  Rainfall(mm)
1970-01-05 00:00:00           1.0 
1970-01-05 01:00:00           1.0
1970-01-05 05:00:00           3.6
1970-01-05 06:00:00           3.6
1970-01-05 07:00:00           2.2
1970-01-05 08:00:00           2.2
1970-01-05 09:00:00           2.2
1970-01-05 10:00:00           2.2
1970-01-05 11:00:00           2.2
1970-01-05 13:00:00           2.2
1970-01-05 13:00:00           2.2
1970-01-05 13:00:00           2.2"

library(zoo)
library(chron)

asChron <- function(d, t) as.chron(paste(d, t))
z <- read.zoo(text = Lines, skip = 1, index = 1:2, FUN = asChron, agg = mean)
merge(z, zoo(, seq(start(z), end(z), 1/24))) # as in FAQ

如果数据来自文件,请将 text = Lines 替换为类似 file = "myfile.dat" 的内容.

If the data comes from a file replace text = Lines with something like file = "myfile.dat" .