Excel or R: Merge time series with missing values

时间:2016-04-04 18:58:51

标签: r excel csv merge time-series

I have multiple somewhat irregular time series (each in a CSV file) like so:

X.csv

date,time,value
01/01/04,00:15:00,4.98
01/01/04,00:25:00,4.981
01/01/04,00:35:00,4.983
01/01/04,00:55:00,4.986

and so:

Y.csv

date,time,value
01/01/04,00:05:00,9.023
01/01/04,00:15:00,9.022
01/01/04,00:35:00,9.02
01/01/04,00:45:00,9.02
01/01/04,00:55:00,9.019

Notice how there's basically a granularity of 10 mins in both files, but each has some missing entries.

I would now like to merge these two time series achieve the following:

date,time,X,Y
01/01/04,00:05:00,NA,9.023
01/01/04,00:15:00,4.98,9.022
01/01/04,00:25:00,4.981,NA
01/01/04,00:35:00,4.983,9.02
01/01/04,00:45:00,NA,9.02
01/01/04,00:55:00,4.986,9.019

Is there an easy way of achieving this? Since I have multiple files (not just two), is there a way of doing this for a batch of files?

2 个答案:

答案 0 :(得分:1)

获取数据:

X <- read.table(pipe("pbpaste"), sep=",", header=T)
X$date <- as.POSIXct(paste(as.Date(X$date, format='%m/%d/%y'),X$time))

让我们

> X
                 date     time value
1 2004-01-01 00:15:00 00:15:00 4.980
2 2004-01-01 00:25:00 00:25:00 4.981
3 2004-01-01 00:35:00 00:35:00 4.983
4 2004-01-01 00:55:00 00:55:00 4.986

与Y相同:

> Y
                 date     time value
1 2004-01-01 00:05:00 00:05:00 9.023
2 2004-01-01 00:15:00 00:15:00 9.022
3 2004-01-01 00:35:00 00:35:00 9.020
4 2004-01-01 00:45:00 00:45:00 9.020
5 2004-01-01 00:55:00 00:55:00 9.019

现在将X,Y转换为xts对象,并将2个对象与outer join合并以获取所有数据点。

result <- merge(as.xts(X[,3],order.by = X$date),as.xts(Y[,3],order.by = Y$date),join='outer’)

names(result) <- c('x','y')

最后一步是按行汇总值:

result$bothXY <- rowSums(result,na.rm=T)

如果您不再需要x,y列:

result <- result[,3]

你得到:

> result
                    bothXY
2004-01-01 00:05:00  9.023
2004-01-01 00:15:00 14.002
2004-01-01 00:25:00  4.981
2004-01-01 00:35:00 14.003
2004-01-01 00:45:00  9.020
2004-01-01 00:55:00 14.005

答案 1 :(得分:-4)

You can use dplyr to do this. First read in all the files from group X and group Y using a do loop, so that you end up with just one file for each. Then full_join the results.