匹配不同长度的时间向量:一个棘手的问题 [英] matching time vectors of different length: a tricky one

查看:25
本文介绍了匹配不同长度的时间向量:一个棘手的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两组来自不同机器的测量值.它们随着时间的推移以略有不同的间隔进行测量 - 例如一个每 5 分钟测量一次,而另一个每 3 分钟测量一次.优点是每 5 分钟一次计算为整个间隔的平均值,因此这些值应该大致对应.我想用每 5 分钟测量一次(光)来扩展向量,使其值与每 5 分钟进行一次测量的向量中的值大致同步.然后应该用前面的值填充间隙

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

这是一个每 5 分钟的数据示例

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 

和每 3 分钟的数据

    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

我也不应该认为光测量的矢量(每 5 分钟)比每 3 分钟的矢量短.因此,目标是使 5 分钟测量的向量与 3 分钟向量的长度相同.

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.

推荐答案

如果我理解正确,这可以通过 zoo 或 xts 轻松完成.首先,这是您的示例数据:

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)

现在我们加载 xts 包,它也加载了 zoo.然后我们将 LightFlux data.frame 对象转换为 xts 对象.

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"))

这是很棒的部分.merge.xtsmerge.zoo 将按索引对齐每个系列.na.locf 用之前的值填充每个 NA.

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)

最后,我们可以从合并后的 Data 对象中提取 3 分钟索引.

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

这篇关于匹配不同长度的时间向量:一个棘手的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆