matching time vectors of different length: a tricky one

别来无恙 提交于 2019-12-05 20:12:39

If I understand correctly, this is easily accomplished with either zoo or xts. First, here's your sample data:

Lines1 <- "Date,Light
26/05/2011 16:00,-529.98
26/05/2011 16:05,-276.68
26/05/2011 16:10,-179.63
26/05/2011 16:15,-385.57
26/05/2011 16:20,-1273.6
26/05/2011 16:25,-1109.7"

Lines2 <- "Date,Flux
26/05/2011 16:01,0.64
26/05/2011 16:04,-1.96
26/05/2011 16:07,-0.51
26/05/2011 16:10,-1.34
26/05/2011 16:13,-1.28
26/05/2011 16:15,-0.22"

con <- textConnection(Lines1)
Light <- read.csv(con, stringsAsFactors=FALSE, header=TRUE)
close(con)
con <- textConnection(Lines2)
Flux <- read.csv(con, stringsAsFactors=FALSE, header=TRUE)
close(con)

Now we load the xts package, which also loads zoo. Then we convert the Light and Flux data.frame objects to xts objects.

library(xts)
light <- xts(Light$Light, as.POSIXct(Light$Date, format="%d/%m/%Y %H:%M"))
flux <- xts(Flux$Flux, as.POSIXct(Flux$Date, format="%d/%m/%Y %H:%M"))

Here's the awesome part. merge.xts and merge.zoo will align each series by index. na.locf fills in each NA with the previous value.

Data <- merge(light,flux)
#                        light  flux
# 2011-05-26 16:00:00  -529.98    NA
# 2011-05-26 16:01:00       NA  0.64
# 2011-05-26 16:04:00       NA -1.96
# 2011-05-26 16:05:00  -276.68    NA
# 2011-05-26 16:07:00       NA -0.51
# 2011-05-26 16:10:00  -179.63 -1.34
# 2011-05-26 16:13:00       NA -1.28
# 2011-05-26 16:15:00  -385.57 -0.22
# 2011-05-26 16:20:00 -1273.60    NA
# 2011-05-26 16:25:00 -1109.70    NA
Data <- na.locf(Data)

Finally, we can extract the 3 minute index from the merged Data object.

Data[index(flux),]
#                       light  flux
# 2011-05-26 16:01:00 -529.98  0.64
# 2011-05-26 16:04:00 -529.98 -1.96
# 2011-05-26 16:07:00 -276.68 -0.51
# 2011-05-26 16:10:00 -179.63 -1.34
# 2011-05-26 16:13:00 -179.63 -1.28
# 2011-05-26 16:15:00 -385.57 -0.22

You can use approx, which will linearly interpolate between your datapoints. Here's a quick example:

x = sort( rnorm(20) )
y = 1:20
plot(x, y, main = 'function interpolation example' )
points(approx(x, y), col = 2, pch = 3 )

To specify how many points you want to interpolate, you can use the xout parameter, like this:

points( approx( x, y, xout = seq( from = min(x), to = max(x), by = 0.1 ) ), pch = 3, col = 3 )

For more interpolation points:

points( approx( x, y, xout = seq( from = min(x), to = max(x), by = 0.05 ) ), pch = 3, col = 4 )

For your specific example, you'd want to do something like interpolating the x,y values of both functions using the intersection of timepoints from both machines. Here is one suggestion:

x_interp = unique( sort( c(seq( from = 0, to = 100, by = 5 ), seq( from = 0, to = 100, by = 3 ) ) ) )
x_interp
 [1]   0   3   5   6   9  10  12  15  18  20  21  24  25  27  30  33  35
[18]  36  39  40  42  45  48  50  51  54  55  57  60  63  65  66  69  70
[35]  72  75  78  80  81  84  85  87  90  93  95  96  99 100

Then, you can use this x_interp as an xout to interpolate between points from both machines:

par( mfrow = c(1,2) )
plot( x_light, y_light )
points(approx(x_light, y_light, x_out = x_interp), col = 2, pch = 3 )

plot( x_flux, y_flux )
points(approx(x_flux, y_flux, x_out = x_interp), col = 3, pch = 3 )

If you'd like to get a function that interpolates values for arbitrary inputs, see the related function called approxfun.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!