# [R] question on creating zoo object from indexed time series: re-arranges dates

Gabor Grothendieck ggrothendieck at gmail.com
Wed Mar 31 02:05:28 CEST 2010

```Here is one more solution.  chron has a global option,
chron.year.expand, that can be set to a function that will do the 2
year to 4 year translation.  We can use it to change the cut.off to 20
(instead of 30).  (Its important to set the option after the
library(chron) call since that call resets the option.)

> library(zoo)
> library(chron)
> options(chron.year.expand = function(y, cut.off = 20, ...) year.expand(y, cut.off, ...))
>
> Lines <- "Date     Time Value
+ 12/31/29 21:00:00   1.4
+ 12/31/29 22:00:00   1.4
+ 12/31/29 23:00:00   1.5
+ 01/01/30 00:00:00   1.6
+ 01/01/30 01:00:00   1.7
+ 01/01/30 02:00:00   1.7
+ 01/01/30 03:00:00   1.8
+ 01/01/30 04:00:00   1.7
+ 01/01/30 05:00:00   1.6
+ 01/01/30 06:00:00   1.5"
> z <- zoo(DF\$Value, chron(DF\$Date, DF\$Time))
> z
(12/31/29 21:00:00) (12/31/29 22:00:00) (12/31/29 23:00:00) (01/01/30 00:00:00)
1.4                 1.4                 1.5                 1.6
(01/01/30 01:00:00) (01/01/30 02:00:00) (01/01/30 03:00:00) (01/01/30 04:00:00)
1.7                 1.7                 1.8                 1.7
(01/01/30 05:00:00) (01/01/30 06:00:00)
1.6

On Tue, Mar 30, 2010 at 5:53 PM, Christina Karamperidou
<ck4545 at gmail.com> wrote:
> thanks a million for solving this problem for me! I really appreciate it.
> best,
> Christina K.
> ck4545 at gmail.com
> P Please consider the environment - Do you really need to print this email?
>
>
> On Mar 30, 2010, at 5:44 PM, Gabor Grothendieck wrote:
>
> The problem is that chron uses 30 as the cutoff year for two digit
> years.  If the year is prior to 30 it adds 2000 to the two digit year
> to get the 4 digit year and if the 2 digit year is 30 or more it adds
> 1900 to the two digit year to get the 4 digit year.  In the line
> marked ######### we convert the two digit years to 4 digit years so
> there is no ambiguity.  (..)\$ matches the last two characters and
> "19\\1" means 19 followed by the first back reference where a back
> reference means the part within parentheses in the match to the
> pattern.
>
> Lines <- "Date     Time Value
>
> + 12/31/29 21:00:00   1.4
> + 12/31/29 22:00:00   1.4
> + 12/31/29 23:00:00   1.5
> + 01/01/30 00:00:00   1.6
> + 01/01/30 01:00:00   1.7
> + 01/01/30 02:00:00   1.7
> + 01/01/30 03:00:00   1.8
> + 01/01/30 04:00:00   1.7
> + 01/01/30 05:00:00   1.6
> + 01/01/30 06:00:00   1.5"
>
> library(zoo)
>
> library(chron)
>
>
> DF\$Date <- sub("(..)\$", "19\\1", DF\$Date)  ##########
>
> z <- zoo(DF\$Value, chron(DF\$Date, DF\$Time))
>
> z
>
> (12/31/29 21:00:00) (12/31/29 22:00:00) (12/31/29 23:00:00) (01/01/30
> 00:00:00)
>                1.4                 1.4                 1.5
>                 1.6
> (01/01/30 01:00:00) (01/01/30 02:00:00) (01/01/30 03:00:00) (01/01/30
> 04:00:00)
>                1.7                 1.7                 1.8
>                 1.7
> (01/01/30 05:00:00) (01/01/30 06:00:00)
>                1.6                 1.5
>
>
> On Tue, Mar 30, 2010 at 5:21 PM, Christina Karamperidou
> <ck4545 at gmail.com> wrote:
>
> true, sorry, I posted a few minutes ago the code for going from 1929 to
>
> 1930, and it is not working on my computer. so, when I use the full record,
>
> z goes from 1930 to 2005, and then jumps to what it perceives to be 2013 and
>
> takes it all the way to 2029 (when in reality it is 1913 to 1929)
>
> Lines <- "Date     Time Value
>
> 12/31/29 21:00:00   1.4
>
> 12/31/29 22:00:00   1.4
>
> 12/31/29 23:00:00   1.5
>
> 01/01/30 00:00:00   1.6
>
> 01/01/30 01:00:00   1.7
>
> 01/01/30 02:00:00   1.7
>
> 01/01/30 03:00:00   1.8
>
> 01/01/30 04:00:00   1.7
>
> 01/01/30 05:00:00   1.6
>
> 01/01/30 06:00:00   1.5"
>
>
> library(zoo)
>
> library(chron)
>
> z <- zoo(DF\$Value, chron(DF\$Date, DF\$Time))
>
> z
>
> (01/01/30 00:00:00) (01/01/30 01:00:00) (01/01/30 02:00:00) (01/01/30
>
> 03:00:00) (01/01/30 04:00:00)
>
>                 1.6                 1.7                 1.7
>
> 1.8                 1.7
>
> (01/01/30 05:00:00) (01/01/30 06:00:00) (12/31/29 21:00:00) (12/31/29
>
> 22:00:00) (12/31/29 23:00:00)
>
>                 1.6                 1.5                 1.4
>
> 1.4                 1.5
>
> Christina K.
>
> ck4545 at gmail.com
>
> P Please consider the environment - Do you really need to print this email?
>
>
> On Mar 30, 2010, at 5:13 PM, Gabor Grothendieck wrote:
>
> What is the problem?  The code in your post seems to work fine:
>
> Lines <- "Date     Time Value
>
> + 12/31/99 22:00:00   1.8
>
> + 12/31/99 23:00:00   1.9
>
> + 01/01/00 00:00:00   1.8
>
> + 01/01/00 01:00:00   1.7
>
> + 01/01/00 02:00:00   1.6
>
> + 01/01/00 03:00:00   1.5
>
> + 01/01/00 04:00:00   1.4
>
> + 01/01/00 05:00:00   1.4"
>
>
> library(zoo)
>
> library(chron)
>
> z <- zoo(DF\$Value, chron(DF\$Date, DF\$Time))
>
> z
>
> (12/31/99 22:00:00) (12/31/99 23:00:00) (01/01/00 00:00:00) (01/01/00
>
> 01:00:00)
>
>                1.8                 1.9                 1.8
>
>                 1.7
>
> (01/01/00 02:00:00) (01/01/00 03:00:00) (01/01/00 04:00:00) (01/01/00
>
> 05:00:00)
>
>                1.6                 1.5                 1.4
>
>                 1.4
>
> On Tue, Mar 30, 2010 at 4:33 PM, Christina Karamperidou
>
> <ck4545 at gmail.com> wrote:
>
>
> So, it works perfectly fine when I do it at the turn of the century, but
>
> still when I try it on my full record, I get the same problem. Maybe it is
>
> reading in the table in a wrong way. I just read it in as
>
>
> maybe I should just cut and paste parts of z and get it to start in 1913...
>
> I really appreciate your time. thx
>
> Lines <- "Date     Time Value
>
> 12/31/99 22:00:00   1.8
>
> 12/31/99 23:00:00   1.9
>
> 01/01/00 00:00:00   1.8
>
> 01/01/00 01:00:00   1.7
>
> 01/01/00 02:00:00   1.6
>
> 01/01/00 03:00:00   1.5
>
> 01/01/00 04:00:00   1.4
>
> 01/01/00 05:00:00   1.4"
>
>
> library(zoo)
>
> library(chron)
>
> z <- zoo(DF\$Value, chron(DF\$Date, DF\$Time))
>
> z
>
> Christina K.
>
> ck4545 at gmail.com
>
> P Please consider the environment - Do you really need to print this email?
>
>
> On Mar 30, 2010, at 3:48 PM, Gabor Grothendieck wrote:
>
> Not for me.
>
> Please provide your data and code in reproducible form as per last
>
> line of every r-help message and the posting guide.
>
> Lines <- "Date     Time Value
>
> + 01/01/13 00:00:00   1.6
>
> + 01/01/13 01:00:00   1.6
>
> + 01/01/13 02:00:00   1.6
>
> + 01/01/13 03:00:00   1.6
>
> + 01/01/13 04:00:00   1.6
>
> + 01/01/13 05:00:00   1.6
>
> + 01/01/13 06:00:00   1.6
>
> + 01/01/13 07:00:00   1.6
>
> + 01/01/13 08:00:00   1.6
>
> + 01/01/13 09:00:00   1.6"
>
>
> library(zoo)
>
> Warning message:
>
> closing unused connection 3 (Lines)
>
> library(chron)
>
> z <- zoo(DF\$Value, chron(DF\$Date, DF\$Time))
>
> z
>
> (01/01/13 00:00:00) (01/01/13 01:00:00) (01/01/13 02:00:00) (01/01/13
>
> 03:00:00)
>
>                1.6                 1.6                 1.6
>
>                 1.6
>
> (01/01/13 04:00:00) (01/01/13 05:00:00) (01/01/13 06:00:00) (01/01/13
>
> 07:00:00)
>
>                1.6                 1.6                 1.6
>
>                 1.6
>
> (01/01/13 08:00:00) (01/01/13 09:00:00)
>
>                1.6                 1.6
>
>
> On Tue, Mar 30, 2010 at 3:32 PM, Christina Karamperidou
>
> <ck4545 at gmail.com> wrote:
>
> Dear all,
>
> I have a time series of daily measurements that starts like this:
>
> KWhourly[1:10,]
>
>       Date     Time Value
>
> 01/01/13 00:00:00   1.6
>
> 01/01/13 01:00:00   1.6
>
> 01/01/13 02:00:00   1.6
>
> 01/01/13 03:00:00   1.6
>
> 01/01/13 04:00:00   1.6
>
> 01/01/13 05:00:00   1.6
>
> 01/01/13 06:00:00   1.6
>
> 01/01/13 07:00:00   1.6
>
> 01/01/13 08:00:00   1.6
>
> 01/01/13 09:00:00   1.6
>
> I am trying to get average daily, monthly, yearly values using the zoo
>
> package.
>
> My series starts at 01/01/13 00:00:00   and ends at   12/31/05 23:00:00
>
> I create the zoo object:
>
> #z <- zoo(KWhourly\$Value,chron(KWhourly\$Date, KWhourly\$Time))
>
> but then z starts at:
>
> start(z)
>
> [1] (01/01/30 00:00:00)
>
> and ends at:
>
> end(z)
>
> [1] (12/31/29 23:00:00)
>
> instead of going from 01/01/13 to 12/31/05
>
> any ideas why this is happening and how to fix it?
>
> I really appreciate your help
>
>
> Christina K.
>
> Columbia University
>
> ck4545 at gmail.com
>
> P Please consider the environment - Do you really need to print this email?
>
>
>
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
>
> R-help at r-project.org mailing list
>
> https://stat.ethz.ch/mailman/listinfo/r-help
>
>
> and provide commented, minimal, self-contained, reproducible code.
>
>
>
>
>
>
>

```