[R] Two similar zoo objects with different structures, how to get same structure?

Gabor Grothendieck ggrothendieck at gmail.com
Wed Jan 21 17:01:50 CET 2009


Is your purpose to change the times in some way?
If z is a zoo series you can change the times like this:

library(zoo)
library(chron)

time(z) <- ...whatever...

e.g.

> z <- zoo(1:3, 11:13)
> z
11 12 13
 1  2  3
> time(z) <- as.Date(11:13)
> z
1970-01-12 1970-01-13 1970-01-14
         1          2          3

On Wed, Jan 21, 2009 at 10:51 AM, Sergey Goriatchev <sergeyg at gmail.com> wrote:
> Dear Dr. Grothendieck,
>
> First of all, I realized I did not load zoo package before I tried the
> first str(bldata). If I load zoo and then do str(bldata) I get the
> following:
>
> 'zoo' series from 7305 to 14609
>  Data: num [1:5219, 1:12] 91.9 91.8 91.7 91.8 91.7 ...
>  - attr(*, "dimnames")=List of 2
>  ..$ : NULL
>  ..$ : chr [1:12] "ED4 COMDTY" "ED12 COMDTY" "ER4 COMDTY" "ER12 COMDTY" ...
>  Index: Classes 'dates', 'times'  atomic [1:5219] 7305 7306 7307 7308 7309 ...
>  ..- attr(*, "format")= chr "m/d/y"
>  ..- attr(*, "origin")= Named num [1:3] 1 1 1970
>  .. ..- attr(*, "names")= chr [1:3] "month" "day" "year"
>
> Now, I've done what you said and here is the ASCII representation of
> the data (I don't know if one can attach files here, and anyway I
> cannot attach files where I am, but the following would reproduce
> exactly):
>
> a <- (structure(c(98.585, 98.355, 98.48, 98.585, 98.67, 98.695, 98.81,
> 98.865, 98.865, 98.865, 98.735, 98.805, 98.805, 97.435, 97.18,
> 97.165, 97.265, 97.34, 97.415, 97.445, 97.505, 97.525, 97.635,
> 97.625, 97.53, 97.53, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25,
> 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 2, 2, 2, 2, 2, 1.5, 1.5,
> 1.5, 1.5, 1.5, 1.5, 1.5, 1.5), .Dim = c(13L, 4L), index = structure(c(14245,
> 14246, 14249, 14250, 14251, 14252, 14253, 14256, 14257, 14258,
> 14259, 14260, 14263), format = "m/d/y", origin = structure(c(1,
> 1, 1970), .Names = c("month", "day", "year")), class = c("dates",
> "times")), class = "zoo", .Dimnames = list(NULL, c("ED4 COMDTY",
> "ED12 COMDTY", "FDTR INDEX", "UKBRBASE INDEX"))))
>
> Copy-paste to R prompt shows following:
>
>       ED4 COMDTY ED12 COMDTY FDTR INDEX UKBRBASE INDEX
> 14245     98.585      97.435       0.25            2.0
> 14246     98.355      97.180       0.25            2.0
> 14249     98.480      97.165       0.25            2.0
> 14250     98.585      97.265       0.25            2.0
> 14251     98.670      97.340       0.25            2.0
> 14252     98.695      97.415       0.25            1.5
> 14253     98.810      97.445       0.25            1.5
> 14256     98.865      97.505       0.25            1.5
> 14257     98.865      97.525       0.25            1.5
> 14258     98.865      97.635       0.25            1.5
> 14259     98.735      97.625       0.25            1.5
> 14260     98.805      97.530       0.25            1.5
> 14263     98.805      97.530       0.25            1.5
>
> On my Bloomberg machine, in R console, the rownames look like:
>           ED4 COMDTY ED12 COMDTY FDTR INDEX UKBRBASE INDEX
> 01/01/09     98.585      97.435       0.25            2.0
> 01/02/09     98.355      97.180       0.25            2.0
> 01/05/09     98.480      97.165       0.25            2.0
> 01/06/09     98.585      97.265       0.25            2.0
> 01/07/09     98.670      97.340       0.25            2.0
>
> and that is what I try to get as well.
>
> Doing:
>
> attributes(a)[[2]] <- format(as.Date(attributes(a)[[2]]), "%m/%d/%y")
>
> changes structure of a to:
>
> 'zoo' series from 01/01/09 to 01/19/09
>  Data: num [1:13, 1:4] 98.6 98.4 98.5 98.6 98.7 ...
>  - attr(*, "dimnames")=List of 2
>  ..$ : NULL
>  ..$ : chr [1:4] "ED4 COMDTY" "ED12 COMDTY" "FDTR INDEX" "UKBRBASE INDEX"
>  Index:  chr [1:13] "01/01/09" "01/02/09" "01/05/09" "01/06/09"
> "01/07/09" "01/08/09" "01/09/09" "01/12/09" "01/13/09" "01/14/09"
> "01/15/09" "01/16/09" "01/19/09"
>
> That is not the same structure, attributes disappeared.
>
> What am I doing wrong?
> Thank you in advance for your help.
>
> Regards,
> Sergey
>
>
>
>
> On Wed, Jan 21, 2009 at 15:56, Gabor Grothendieck
> <ggrothendieck at gmail.com> wrote:
>> Please reduce your examples down to small amounts of data and use dput so
>> that they are reproducible.
>>
>> On Wed, Jan 21, 2009 at 9:32 AM, Sergey Goriatchev <sergeyg at gmail.com> wrote:
>>> Dear all,
>>>
>>> I have a zoo object that has following structure:
>>>
>>>> str(bldata)
>>>  zoo [1:5219, 1:12] 91.9 91.8 91.7 91.8 91.7 ...
>>>  - attr(*, "index")=Classes 'dates', 'times'  atomic [1:5219] 7305
>>> 7306 7307 7308 7309 ...
>>>  .. ..- attr(*, "format")= chr "m/d/y"
>>>  .. ..- attr(*, "origin")= Named num [1:3] 1 1 1970
>>>  .. .. ..- attr(*, "names")= chr [1:3] "month" "day" "year"
>>>  - attr(*, "dimnames")=List of 2
>>>  ..$ : NULL
>>>  ..$ : chr [1:12] "ED4 COMDTY" "ED12 COMDTY" "ER4 COMDTY" "ER12 COMDTY" ...
>>>
>>> I also have a vector of dates of same length as zoo object (5219):
>>>
>>>> str(bldates)
>>>  chr [1:5219] "01/01/90" "01/02/90" "01/03/90" "01/04/90" "01/05/90"
>>> "01/08/90" "01/09/90" "01/10/90" "01/11/90" "01/12/90" ...
>>>
>>> I do the following:
>>>
>>>> attributes(bldata)[[2]] <- as.Date(bldates, "%m/%d/%y")
>>>
>>> and get the following:
>>>
>>>> str(bldata)
>>> 'zoo' series from 1990-01-01 to 2009-12-31
>>>  Data: num [1:5219, 1:12] 91.9 91.8 91.7 91.8 91.7 ...
>>>  - attr(*, "dimnames")=List of 2
>>>  ..$ : NULL
>>>  ..$ : chr [1:12] "ED4 COMDTY" "ED12 COMDTY" "ER4 COMDTY" "ER12 COMDTY" ...
>>>  Index: Class 'Date'  num [1:5219] 7305 7306 7307 7308 7309 ...
>>>
>>> But what I really want to get is the following:
>>>
>>>>str(bldata)
>>> 'zoo' series from 01/01/90 to 12/31/09
>>>  Data: num [1:5219, 1:12] 91.9 91.8 91.7 91.8 91.7 ...
>>>  - attr(*, "dimnames")=List of 2
>>>  ..$ : NULL
>>>  ..$ : chr [1:12] "ED4 COMDTY" "ED12 COMDTY" "ER4 COMDTY" "ER12 COMDTY" ...
>>>  Index: Classes 'dates', 'times'  atomic [1:5219] 7305 7306 7307 7308 7309 ...
>>>  ..- attr(*, "format")= chr "m/d/y"
>>>  ..- attr(*, "origin")= Named num [1:3] 1 1 1970
>>>  .. ..- attr(*, "names")= chr [1:3] "month" "day" "year"
>>>
>>> The array bldata is the same, only the last one is on a machine
>>> running RBloomberg, and then I tried to save bldata (and rownames of
>>> bldata in bldates) and
>>> transfer bldata to the other machine which is not a Bloomberg
>>> terminal, and the structure of bldata changes.
>>>
>>> What should I do in this case to adjust the structure? I guess I have
>>> to somehow change the class of Index and set attributes of "Index" and
>>> attribute "names" of attribute "origin" of attribute "Index"?
>>>
>>> Thank you for your help in advance!
>>>
>>> Regards,
>>> Sergey
>>>
>>> --
>>> I'm not young enough to know everything. /Oscar Wilde
>>> Experience is one thing you can't get for nothing. /Oscar Wilde
>>> When you are finished changing, you're finished. /Benjamin Franklin
>>> Tell me and I forget, teach me and I remember, involve me and I learn.
>>> /Benjamin Franklin
>>> Luck is where preparation meets opportunity. /George Patten
>>>
>>> ______________________________________________
>>> 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.
>>>
>>
>
>
>
> --
> I'm not young enough to know everything. /Oscar Wilde
> Experience is one thing you can't get for nothing. /Oscar Wilde
> When you are finished changing, you're finished. /Benjamin Franklin
> Tell me and I forget, teach me and I remember, involve me and I learn.
> /Benjamin Franklin
> Luck is where preparation meets opportunity. /George Patten
>




More information about the R-help mailing list