合并两个具有不同时间粒度的时间序列 [英] merge two time series with different time granularities

查看:179
本文介绍了合并两个具有不同时间粒度的时间序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个包含时间序列数据的CsV文件.我想将两者合并为一个文件. File1具有1分钟间隔的定期数据. File2具有非周期性的事件触发数据. File2中数据的时间戳可能与File1中数据的时间戳一致,也可能不一致.我想合并两个数据集以创建其时间戳是Data1和Data2的并集的数据集.对于两者都不通用的时间戳,我希望将相应数据集的缺失条目表示为NA.

I have two CsV files containing time series data. I want to merge the two into a single file. File1 has periodic data at 1-minute intervals. File2 has event-triggered data that is not periodic. The timestamps for data in File2 may or may not coincide with data in File1. I want to merge the two datasets to create a dataset whose timestamps are a union of Data1 and Data2. For timestamps that are not common to both, I want the missing entries for the corresponding dataset to be indicated as NA.

这是File1的示例输入:

Here is a sample input for File1:

     Time                 A1  A2       
  2013-08-05 00:00:00     2   1               
  2013-08-05 00:01:00     2   1                      
  2013-08-05 00:02:00     1   1                

这是File2的示例输入:

Here is a sample input for File2:

     Time               B1     B2       B3
2013-08-01 12:10:21       5     1          1    
2013-08-05 00:02:00       5     1          1                
2013-08-05 12:13:44      14     1          2   

预期输出如下:

  Time                 A1   A2      B1     B2       B3
2013-08-01 12:10:21    NA    NA     5     1          1    
2013-08-05 00:00:00    2     1      NA    NA         NA  
2013-08-05 00:01:00    2     1      NA    NA         NA        
2013-08-05 00:02:00    1      1     5     1          1 
2013-08-05 12:13:44    NA    NA     14     1          2  

我使用了merge.zoo,还尝试了该论坛中其他相关帖子所建议的merge.xts.但是我没有得到预期的输出.这是我使用的代码.

I used merge.zoo and also tried merge.xts as suggested by other relevant posts in this forum. But I am not getting the expected output. Here is the code I used.

A <- read.zoo(read.csv("File1.csv", header=TRUE));
B <- read.zoo(read.csv("File2.csv", header=TRUE));
C <- merge.zoo(A,B);

希望您能提供任何帮助.谢谢.

I would appreciate any help you can provide. Thank you.

推荐答案

应替换文件中读取的行,如下所示:

The lines that read in the files should be replaced as shown:

> A <- read.zoo("File1.csv", header = TRUE, tz = "", sep = ",")
> B <- read.zoo("File2.csv", header = TRUE, tz = "", sep = ",")
> merge(A, B)
                    A1 A2 B1 B2 B3
2013-08-01 12:10:21 NA NA  5  1  1
2013-08-05 00:00:00  2  1 NA NA NA
2013-08-05 00:01:00  2  1 NA NA NA
2013-08-05 00:02:00  1  1  5  1  1
2013-08-05 12:13:44 NA NA 14  1  2

此处为可复制形式:

Lines1 <- "   Time,                A1,  A2       
  2013-08-05 00:00:00,    2,  1               
  2013-08-05 00:01:00,    2,  1                      
  2013-08-05 00:02:00,    1,  1  
"

Lines2 <- " Time,              B1,    B2,      B3
2013-08-01 12:10:21,       5,    1,          1    
2013-08-05 00:02:00,      5,    1,          1                
2013-08-05 12:13:44,     14,    1,          2   
"

library(zoo)
A <- read.zoo(text = Lines1, header = TRUE, tz = "", sep = ",")
B <- read.zoo(text = Lines2, header = TRUE, tz = "", sep = ",")
merge(A, B)

这篇关于合并两个具有不同时间粒度的时间序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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