Skip to content
Prev 167734 / 398502 Next

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

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: