[R] can't merge zoo ojects and convert to ts (been trying for 2 days)

Gabor Grothendieck ggrothendieck at gmail.com
Fri Mar 7 03:42:55 CET 2008


Its a bug in na.approx.  I just fixed it and (1) until a
new version of zoo comes out add this to your code:

source("http://r-forge.r-project.org/plugins/scmsvn/viewcvs.php/*checkout*/pkg/R/na.approx.R?rev=448&root=zoo")

or (2) a workaround not requiring that source statement
is to add the na.rm = FALSE argument to
na.approx since the bug only gets triggered with the
default na.rm = TRUE setting.

Actually in your case you don't need na.approx anyways
since as.ts can do it all itself and if you want to force
a specific frequency (rather than the one it picks) you
can convert to zooreg first specifying that frequency.

Also please try to provide code in such a manner that
one can simply copy it from the post and paste it into
one's R session to reproduce the problem using the
style shown below.  I have also illustrated some
simplifications:

library(zoo)
library(chron)

la_time_test2 <- "TRIP_ID,Ruta_datetime_start,Minutos de viaje
28/04/2004_CM2-CM1_downriver_08:15,28/04/2004 08:15,125
28/04/2004_CM1-CM2_upriver_13:05,28/04/2004 13:05,295
"

t.df <- read.csv(textConnection(la_time_test2))
t.zoo <- zoo(test2.df[,3], as.chron(strptime(test2.df[,2], "%d/%m/%Y %H:%M")))
t.ts <- as.ts(as.zooreg(test2.zoo, freq = 24 * 365))


On Thu, Mar 6, 2008 at 5:00 PM, Darren Norris <doon75 at hotmail.com> wrote:
>
> I'm stuck, but am sure it can be done I just don't understand how.
> I have data in an irregular timeseries. I want to be able to use stl to
> visualise the data (see seasonal parts etc), so I need to change to regular
> series of class ts (I think).
>
> I am using 2 zoo objects one is regular and the other is my irregular data.
> I am then merging to create the object I want but when I try to change to a
> ts class using as.ts I get the following error:
> Error in zoo(coredata(x), tt) :
>  "x" : attempt to define illegal zoo object.
>
> Can anyone show the error of my ways? I have looked throuh help and the zoo
> pdf's but can't see the answer.
> Many thanks for any help.
> All code and data below (short example).
> Darren
>
> R version 2.6.1 (2007-11-26)
> i386-pc-mingw32
>
> locale:
> LC_COLLATE=English_United Kingdom.1252;LC_CTYPE=English_United
> Kingdom.1252;LC_MONETARY=English_United
> Kingdom.1252;LC_NUMERIC=C;LC_TIME=English_United Kingdom.1252
>
> attached base packages:
> [1] stats     graphics  grDevices utils     datasets  methods   base
>
> other attached packages:
> [1] chron_2.3-21 zoo_1.4-2
>
> loaded via a namespace (and not attached):
> [1] grid_2.6.1     lattice_0.17-2 tools_2.6.1
>
> ###Import data:
>  ttest<-read.csv("la_time_test2.csv", header = T, as.is=TRUE);
>  tlist<-read.csv("la_time_list2.csv", header = T, as.is=TRUE)
>
> ####Convert to zoo:
>  zooreg_time<-zoo(, as.chron(strptime(tlist$a_datetime, format="%d/%m/%Y
> %H"), format = c(dates = "Y-m-d ", times = "H:M:S")),frequency=8760 )
>  class(zooreg_time)
> [1] "zooreg" "zoo"
>
> ###Create my zoo objects
> ###First irregular series. Steps: 1) create vector to order by 2) create zoo
> object.
>
> #####Vector to order by:
>
> >zoo_test<- as.chron(strptime(ttest$Ruta_datetime_start, format="%d/%m/%Y
> %H"), format = c(dates = "Y-m-d ", times = "H:M:S"))
> zoo_test
> [1] (2004-Apr-28 08:00:00) (2004-Apr-28 13:00:00)
>
> #####Zoo object:
> zoo_test2<-zoo(ttest$Minutos.de.viaje,zoo_test)
>  zoo_test2
> (2004-Apr-28 08:00:00) (2004-Apr-28 13:00:00)
>                   125                    295
>
> #####Merge (need to approximate NAs so can change to ts?)
> x<-na.approx(merge(zooreg_time,zoo_test2))
> x
> (2004-Apr-28 08:00:00) (2004-Apr-28 09:00:00) (2004-Apr-28 10:00:00)
> (2004-Apr-28 11:00:00) (2004-Apr-28 12:00:00) (2004-Apr-28 13:00:00)
>                   125                    159                    193
> 227                    261                    295
>
> ######Now change to ts
> as.ts(x)
> Error in zoo(coredata(x), tt) :
>  "x" : attempt to define illegal zoo object
>
> THE DATA
> la_time_test2.csv
> TRIP_ID Ruta_datetime_start     Minutos de viaje
> 28/04/2004_CM2-CM1_downriver_08:15      28/04/2004 08:15        125
> 28/04/2004_CM1-CM2_upriver_13:05        28/04/2004 13:05        295
>
> la_time_list2.csv
> a_datetime
> 28/04/2004 01:00
> 28/04/2004 02:00
> 28/04/2004 03:00
> 28/04/2004 04:00
> 28/04/2004 05:00
> 28/04/2004 06:00
> 28/04/2004 07:00
> 28/04/2004 08:00
> 28/04/2004 09:00
> 28/04/2004 10:00
> 28/04/2004 11:00
> 28/04/2004 12:00
> 28/04/2004 13:00
> 28/04/2004 14:00
> 28/04/2004 15:00
> 28/04/2004 16:00
> 28/04/2004 17:00
>
> ttest
>                             TRIP_ID Ruta_datetime_start Minutos.de.viaje
> 1 28/04/2004_CM2-CM1_downriver_08:15    28/04/2004 08:15              125
> 2   28/04/2004_CM1-CM2_upriver_13:05    28/04/2004 13:05              295
>
> tlist
>         a_datetime
> 1  28/04/2004 01:00
> 2  28/04/2004 02:00
> 3  28/04/2004 03:00
> 4  28/04/2004 04:00
> 5  28/04/2004 05:00
> 6  28/04/2004 06:00
> 7  28/04/2004 07:00
> 8  28/04/2004 08:00
> 9  28/04/2004 09:00
> 10 28/04/2004 10:00
> 11 28/04/2004 11:00
> 12 28/04/2004 12:00
> 13 28/04/2004 13:00
> 14 28/04/2004 14:00
> 15 28/04/2004 15:00
> 16 28/04/2004 16:00
> 17 28/04/2004 17:00
>
> zooreg_time
> Data:
> numeric(0)
> Index:
>  [1] (2004-Apr-28 01:00:00) (2004-Apr-28 02:00:00) (2004-Apr-28 03:00:00)
> (2004-Apr-28 04:00:00) (2004-Apr-28 05:00:00) (2004-Apr-28 06:00:00)
>  [7] (2004-Apr-28 07:00:00) (2004-Apr-28 08:00:00) (2004-Apr-28 09:00:00)
> (2004-Apr-28 10:00:00) (2004-Apr-28 11:00:00) (2004-Apr-28 12:00:00)
> [13] (2004-Apr-28 13:00:00) (2004-Apr-28 14:00:00) (2004-Apr-28 15:00:00)
> (2004-Apr-28 16:00:00) (2004-Apr-28 17:00:00)
>
>
> x
> (2004-Apr-28 08:00:00) (2004-Apr-28 09:00:00) (2004-Apr-28 10:00:00)
> (2004-Apr-28 11:00:00) (2004-Apr-28 12:00:00) (2004-Apr-28 13:00:00)
>                   125                    159                    193
> 227                    261                    295
>
>
> --
> View this message in context: http://www.nabble.com/can%27t-merge-zoo-ojects-and-convert-to-ts-%28been-trying-for-2-days%29-tp15885132p15885132.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list