ToNoY
ToNoY

Reputation: 1378

merging two time-series into a new data frame using R

Hi I have a small discontinuous time-series data (mWL):

> print(mWL)
                   dt    Q1    Q2    R1    R2 S1 S2 S3    N1    N2     O
1 2013-05-25 12:00:00    NA    NA    NA    NA NA NA NA    NA    NA    NA
2 2013-05-25 13:20:00    NA    NA    NA    NA NA NA NA    NA    NA    NA
3 2013-05-25 15:20:00    NA    NA 4.107 4.167 NA NA NA    NA    NA    NA
4 2013-05-25 15:40:00 5.833 6.405    NA    NA NA NA NA    NA    NA    NA
5 2013-05-25 17:00:00    NA    NA    NA    NA NA NA NA    NA    NA 6.957
6 2013-05-25 17:20:00    NA    NA    NA    NA NA NA NA 6.088 7.307    NA

And I also have a rather big continuous (every 20 min) database (H) which also contains a few of the time measuremnts of 'mWL'

tail(H,n=80)
                     time      e1
13782 2013-05-25 09:40:00 12.8452
13783 2013-05-25 10:00:00 12.8429
13784 2013-05-25 10:20:00 12.8376
13785 2013-05-25 10:40:00 12.8362
13786 2013-05-25 11:00:00 12.8338
13787 2013-05-25 11:20:00 12.8359
13788 2013-05-25 11:40:00 12.8371
13789 2013-05-25 12:00:00 12.8380
13790 2013-05-25 12:20:00 12.8355
13791 2013-05-25 12:40:00 12.8380
13792 2013-05-25 13:00:00 12.8396
13793 2013-05-25 13:20:00 12.8418
13794 2013-05-25 13:40:00 12.8403
13795 2013-05-25 14:00:00 12.8427
13796 2013-05-25 14:20:00 12.8443
13797 2013-05-25 14:40:00 12.8453
13798 2013-05-25 15:00:00 12.8460
13799 2013-05-25 15:20:00 12.8483
13800 2013-05-25 15:40:00 12.8508
13801 2013-05-25 16:00:00 12.8528
13802 2013-05-25 16:20:00 12.8547
13803 2013-05-25 16:40:00 12.8559
13804 2013-05-25 17:00:00 12.8579
13805 2013-05-25 17:20:00 12.8594
13806 2013-05-25 17:40:00 12.8613

I want to make a new data frame of the size of 'mWL' (i.e. only 6 row) with H$e1 data merged on same time; but when I try to use align.time, the data frame remains big and the 'mWL' data repeats!!

require(xts)
Hsort<-align.time(xts(H[,2],as.POSIXct(H[,1])), n=1200)
mWLsort<-align.time(xts(mWL[,2],as.POSIXct(mWL[,1])), n=1200)
merge(H, mWLsort)

any suggestion??

Upvotes: 0

Views: 1133

Answers (1)

IRTFM
IRTFM

Reputation: 263331

The default for all is TRUE for merge.zoo from which merge.xts (if it exists) probably either inherits or follows the lead of the zoo authors. So set all = c(FALSE, TRUE) if you just want the items that are in the second object to be matched. (This is the opposite of the default setting for all in base::merge so I can certainly understand if you were confused. I was until I looked at:

 help(package="zoo", merge.zoo)
 help(package="xts", merge.xts)

Upvotes: 2

Related Questions