问题
I have two sets of measurements from different machines. They are measured over time, at slightly different intervals - e.g. one makes a measurement every 5 mins, but the other, every 3 mins. The advantage is that the one every 5 mins is computed as an average over the whole interval so the values should correspond roughly to one another. I would like to expand the vector with measurements every 5 minutes (Light) so that its values are roughly synchronous with the values in vector of measurements made every 5 minutes. The gap should then be filled with the preceding value
Here is an example of the data every 5 minutes
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
and the data every 3 minutes
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
I should also not that the vector of light measurement (every 5 mins) is shorter than the vector every 3 minutes. The goal is thus to make the vector of 5 min measurements the same length as the 3 minute vector.
I realise that this is quite a tricky problem, but any suggestions would be greatfuly received.
回答1:
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
回答2:
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.
来源:https://stackoverflow.com/questions/6457818/matching-time-vectors-of-different-length-a-tricky-one