[R] Merge failure using zoo package
Gabor Grothendieck
ggrothendieck at gmail.com
Wed Apr 7 13:11:32 CEST 2010
On Wed, Apr 7, 2010 at 6:41 AM, e-letter <inpost at gmail.com> wrote:
> I tried your instructions, as shown.
>
>> library(chron)
>> library(zoo)
>> z1<-read.zoo("test1.csv",header=TRUE,sep=",",FUN=times)
>> z2<-read.zoo("test2.csv",header=TRUE,sep=",",FUN=times)
>> z12<-merge(z1,z2)
>> z3<-na.approx(z12,xout=time(z1))
> Error in approx(along[!na], y[!na], along[na], ...) :
> invalid interpolation method
> In addition: Warning message:
> the condition has length > 1 and only the first element will be used
> in: if (is.na(method)) stop("invalid interpolation method")
>> source("http://r-forge.r-project.org/plugins/scmsvn/viewcvs.php/*checkout*/pkg/zoo/R/na.approx.R?rev=676&root=zoo")
>> z3<-na.approx(z12,xout=time(z1))
>> z3
> z1 z2
> 01:01:30 0.34860813 0.21977817
> 01:02:00 0.61306418 0.46662656
> 01:02:30 0.01495898 0.75482666
> 01:03:00 0.27035612 0.73832408
> ...
>
> It looks that the data of z3 shows only values corresponding to times
> in z1. In the previous merge, z3 consisted of values for times in both
Yes, when xout is specified it says that the result should be
evaluated only at the times specified in xout. . Read about the xout
argument of na.approx and approx at ?approx .
> z1 and z2, e.g.:
>
> z1[, 2] z2[, 2]
> 01:01:01 0.54186455 0.17558467
> 01:01:30 0.34860813 0.20682491
> 01:01:42 0.48083615 0.23806514
> 01:02:00 0.61306418 0.49837120
>
> With respect to advice about seeking help, I think it would be very
> good if your comments about using the command 'textConnection()' were
> added to the FAQ section or the posting guide, to help novices improve
> the quality of asking for help.
>
OK. I have attached the example in a self contained style and will
post something suggesting that the maintainers of the posting guide
incorporate it.
-------------- next part --------------
Lines1 <- "time1,dataset1
01:01:00,0.73512097
01:01:30,0.34860813
01:02:00,0.61306418
01:02:30,0.01495898
01:03:00,0.27035612
01:03:30,0.69513898
01:04:00,0.46451758
01:04:30,0.61672569
01:05:00,0.82496122
01:05:30,0.34766154
01:06:00,0.69618714
01:06:30,0.39035214
01:07:00,0.01680143
01:07:30,0.28576967
01:08:00,0.01205416
01:08:30,0.89637254
01:09:00,0.63147653
01:09:30,0.01522139
01:10:00,0.27661960
01:10:30,0.50974124
01:11:00,0.68141977
01:11:30,0.90725854
01:12:00,0.83823443
01:12:30,0.53360241
01:13:00,0.17769196
01:13:30,0.83438616
01:14:00,0.67248807
01:14:30,0.09991933
01:15:00,0.03334966
01:15:30,0.93292355
01:16:00,0.15990837
01:16:30,0.05354050
01:17:00,0.55281203
01:17:30,0.37845690
01:18:00,0.89051365
01:18:30,0.16674292
01:19:00,0.85458626
01:19:30,0.19278550
01:20:00,0.73240405
01:20:30,0.16417524
01:21:00,0.73878212
01:21:30,0.51790118
01:22:00,0.83076438
01:22:30,0.47555504
01:23:00,0.02108640
01:23:30,0.82911053
01:24:00,0.95555646
01:24:30,0.14493657
01:25:00,0.84422332
01:25:30,0.41589974
01:26:00,0.67606367
01:26:30,0.00606434
01:27:00,0.59951991
01:27:30,0.43949260"
Lines2 <- "time2,dataset2
01:01:01,0.17558467
01:01:42,0.23806514
01:02:23,0.75867726
01:03:06,0.73502357
01:03:50,0.94018206
01:04:35,0.61882643
01:05:21,0.68417492
01:06:08,0.05744461
01:06:55,0.33344394
01:07:44,0.68752593
01:08:33,0.17270469
01:09:23,0.81522124
01:10:03,0.68304352
01:10:43,0.38774082
01:11:23,0.84176890
01:12:04,0.09344446
01:12:44,0.13431965
01:13:25,0.92210721
01:14:06,0.33630635
01:14:47,0.56690294
01:15:29,0.09870816
01:16:11,0.77864105
01:16:53,0.61803441
01:17:35,0.09133728
01:18:17,0.08925487
01:19:00,0.89271117
01:19:42,0.56605742
01:20:25,0.98520534
01:21:08,0.66104843
01:21:51,0.96948589
01:22:34,0.05692690
01:23:17,0.71887456
01:24:00,0.14903741
01:24:43,0.86569445
01:25:26,0.27923513
01:26:09,0.98365033
01:26:53,0.08308399
01:27:36,0.87071027
01:28:19,0.26475705
01:29:03,0.76409811
01:29:47,0.59563256
01:30:31,0.23995054
01:31:14,0.00951054
01:31:59,0.21367270"
library(chron)
library(zoo)
z1<-read.zoo(textConnection(Lines1),header=TRUE,sep=",",FUN=times)
z2<-read.zoo(textConnection(Lines2),header=TRUE,sep=",",FUN=times)
z12 <- merge(z1, z2)
# source("http://r-forge.r-project.org/plugins/scmsvn/viewcvs.php/*checkout*/pkg/zoo/R/na.approx.R?rev=676&root=zoo")
z3<-na.approx(z12,xout=time(z1))
More information about the R-help
mailing list