R 约束间隙的插值

R 约束间隙的插值,r,zoo,seq,R,Zoo,Seq,继续提出以下问题: 我有下表: Lines <- "D1,Diff 1,20/11/2014 16:00,0.01 2,20/11/2014 17:00,0.02 3,20/11/2014 19:00,0.03 <-- Gap I 4,21/11/2014 16:00,0.04 5,21/11/2014 17:00,0.06 <-- Gap II 6,21/11/2014 20:00,0.10" 行源1:。mnel于2012年9月13日回答,马特·道尔于2012年9月13日

继续提出以下问题:

我有下表:

Lines <- "D1,Diff
1,20/11/2014 16:00,0.01
2,20/11/2014 17:00,0.02
3,20/11/2014 19:00,0.03 <-- Gap I
4,21/11/2014 16:00,0.04
5,21/11/2014 17:00,0.06 <-- Gap II
6,21/11/2014 20:00,0.10"
行源1:。mnel于2012年9月13日回答,马特·道尔于2012年9月13日编辑

&

来源2:。mnel于2012年9月13日作出答复,Dirk Eddelbuettel于2012年5月3日进行编辑

library(zoo)
library(xts)
library(data.table)
library(devtools)
devtools::install_github("iembry-USGS/ie2misc")
library(ie2misc)
# iembry released a version of ie2misc so you should be able to install
# the package now
# `na.interp1` is a function that combines zoo's `na.approx` and pracma's
# `interp1`
其余代码在创建z
zoo
对象后开始

## Source 1 begins
startdate <- as.character((start(z)))
# set the start date/time as the 1st entry in the time series and make
# this a character vector.

start <- as.POSIXct(startdate)
# transform the character vector to a POSIXct object

enddate <- as.character((end(z)))
# set the end date/time as the last entry in the time series and make   
# this a character vector.

end <- as.POSIXct(enddate)
# transform the character vector to a POSIXct object

gridtime <- seq(from = start, by = 3600, to = end)
# create a sequence beginning with the start date/time with a 60 minute 
# interval ending at the end date/time
## Source 1 ends

## Source 2 begins
timeframe <- data.frame(rep(NA, length(gridtime)))
# create 1 NA column spaced out by the gridtime to complement the single 
# column of z

timelength <- xts(timeframe, order.by = gridtime)
# create a xts time series object using timeframe and gridtime

zDate <- merge(timelength, z)
# merge the z zoo object and the timelength xts object  
## Source 2 ends

我们可以将
z
与基于小时网格的零宽度动物园系列
z0
合并。这将通过NAs将
z
转换为每小时一次的序列。然后使用
maxgap
参数到
na.approx
,如下所示,仅填充所需的间隙。这仍然使NAs处于较长的间隙中,因此请使用
na删除它们。忽略

fortify.zoo(z3)
会将结果转换为数据帧,但由于
z3
,生成的序列只有长度3的间隙,是一个时间序列,这可能不是一个好主意,最好将其作为zoo对象,以便您可以使用zoo的所有设施

除动物园外,不得使用其他包装

z0 <- zoo(, seq(start(z), end(z), "hours"))
z3 <- na.omit(na.approx(merge(z, z0), maxgap = 3))

谢谢@席胡胡,我得到以下错误和警告:> XIN PSP1>源(“”)源错误(“”):意外输入1:IytRo.i使用以下命令:DeVoTo::SudithGithub(“iBuryUSGS/IE2MISC”)库(IMPRY)并获得:> DeVoToo::loadNamespace中的(“iembry USGS/ie2misc”)错误(j>library(iembry)库中的错误(iembry):没有名为“iembry”的包您的结果包括
21/11/2014 20:00
的插值,但该时间点已经是原始数据的一部分。您的意思是仅插值
21/11/2014 18:00
21/11/2014 19:00
## Source 1 begins
startdate <- as.character((start(z)))
# set the start date/time as the 1st entry in the time series and make
# this a character vector.

start <- as.POSIXct(startdate)
# transform the character vector to a POSIXct object

enddate <- as.character((end(z)))
# set the end date/time as the last entry in the time series and make   
# this a character vector.

end <- as.POSIXct(enddate)
# transform the character vector to a POSIXct object

gridtime <- seq(from = start, by = 3600, to = end)
# create a sequence beginning with the start date/time with a 60 minute 
# interval ending at the end date/time
## Source 1 ends

## Source 2 begins
timeframe <- data.frame(rep(NA, length(gridtime)))
# create 1 NA column spaced out by the gridtime to complement the single 
# column of z

timelength <- xts(timeframe, order.by = gridtime)
# create a xts time series object using timeframe and gridtime

zDate <- merge(timelength, z)
# merge the z zoo object and the timelength xts object  
## Source 2 ends
Lines <- as.data.frame(zDate)
# to data.frame from zoo

Lines[, "D1"] <- rownames(Lines)
# create column named D1

Lines <- setDT(Lines)
# create data.table out of data.frame

setcolorder(Lines, c(3, 2, 1))
# set the column order as the 3rd column followed by the 2nd and 1st 
# columns

Lines <- Lines[, 3 := NULL]
# remove the 3rd column

setnames(Lines, 2, "diff")
# change the name of the 2nd column to diff

Lines <- setDF(Lines)
# return to data.frame

rowsinterps1 <- which(is.na(Lines$diff == TRUE))
# index of rows of Lines that have NA (to be interpolated)

xi <- as.numeric(Lines[which(is.na(Lines$diff == TRUE)), 1])
# the Date-Times for diff to be interpolated in numeric format

interps1 <- na.interp1(as.numeric(Lines$Time), Lines$diff, xi = xi,
na.rm = FALSE, maxgap = 3)
# the interpolated values where only gap sizes of 3 are filled

Lines[rowsinterps1, 2] <- interps1
# replace the NAs in diff with the interpolated diff values

Lines <- na.omit(Lines) # remove rows with NAs
Lines
Lines
                D1       diff
1  2014-11-20 16:00:00 0.01000000
2  2014-11-20 17:00:00 0.02000000
3  2014-11-20 18:00:00 0.02500000
4  2014-11-20 19:00:00 0.03000000
25 2014-11-21 16:00:00 0.04000000
26 2014-11-21 17:00:00 0.06000000
27 2014-11-21 18:00:00 0.07333333
28 2014-11-21 19:00:00 0.08666667
29 2014-11-21 20:00:00 0.10000000
z0 <- zoo(, seq(start(z), end(z), "hours"))
z3 <- na.omit(na.approx(merge(z, z0), maxgap = 3))
> z3
2014-11-20 16:00:00 2014-11-20 17:00:00 2014-11-20 18:00:00 2014-11-20 19:00:00 
         0.01000000          0.02000000          0.02500000          0.03000000 
2014-11-21 16:00:00 2014-11-21 17:00:00 2014-11-21 18:00:00 2014-11-21 19:00:00 
         0.04000000          0.06000000          0.07333333          0.08666667 
2014-11-21 20:00:00 
         0.10000000