Skip to content

How can I declare an empty zoo object?

7 messages · John, arun, R. Michael Weylandt +1 more

#
On Mon, Jul 30, 2012 at 11:18 PM, jpm miao <miaojpm at gmail.com> wrote:
This sounds like the start of a bad idea....
Yep: the real answer is "don't do that." Since R is
copy-on-write+pass-by-reference and a few other nice things, this will
be painfully gut-wrenchingly paint-dryingly slow :-) Much better is to
make all your "x1zoo_f" objects and cbind them at once.

The perils of this idea and the appropriate fixings thereof have been
discussed here there and everywhere, but I think Pat Burn's
presentation in "the R Inferno" is the best (and certainly the most
fun) -- it's a worthwhile read and, perhaps an even higher compliment,
a worthwhile re-read.

Based on my rough memories of your recent posts, you won't understand
it all in the first pass (few do -- I certainly don't), but over time
you'll gain immensely from the work of one of R's wisest Virgils.
For completeness,

xzoo <- zoo()

But like I said: don't do that.

Cheers,
Michael
#
Hi,

When you cbind two zoo objects, I guess the index should match, otherwise it gives warning messages:

x1zoo_f<-zoo(rnorm(5,25),c(1,5,10,15,9))
xzoo<-zoo(c(5,9,10,15),c(1,5,9,10,15))
cbind(xzoo,x1zoo_f)
?? xzoo? x1zoo_f
1???? 5 24.85877
5???? 9 25.09264
9??? 10 25.79896
10?? 15 26.70625
15??? 5 24.63533
#If index is different
xzoo<-zoo(c(5,9,10,15,25), 1:5)
?cbind(xzoo,x1zoo_f)
?? xzoo? x1zoo_f
1???? 5 24.85877
2???? 9?????? NA
3??? 10?????? NA
4??? 15?????? NA
5??? 25 25.09264
9??? NA 25.79896
10?? NA 26.70625
15?? NA 24.63533
Warning message:
In merge.zoo(..., all = all, fill = fill, suffixes = suffixes, retclass = "zoo",? :
? Index vectors are of different classes: integer numeric
######## now, with empty zoo object with same index
xzoo<-zoo(,c(1,5,9,10,15))
?str(xzoo)
?zoo? series (without observations)
?cbind(xzoo,x1zoo_f)
??? x1zoo_f
1? 24.85877
5? 25.09264
9? 25.79896
10 26.70625
15 24.63533

A.K.




From: jpm miao <miaojpm at gmail.com>

To: r-help <r-help at r-project.org>
Cc: 
Sent: Tuesday, July 31, 2012 12:18 AM
Subject: [R] How can I declare an empty zoo object?

Hi,

?  I let xzoo be an empty? object:
?  and I have an existing zoo object x1zoo_f. I would like to combine
the two to make a new zoo object, and continue doing so in a loop,
which is not shown here. However, when I type
?  An error message emerges


Error in zoo(structure(x, dim = dim(x)), index(x), ...) :
? ?x? : attempt to define invalid zoo object


?  Is there any way to define an empty zoo object?


? Thanks,


Miao

??? [[alternative HTML version deleted]]


______________________________________________
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.
#
Hi,

#Without indices

?xzoo<-zoo()
?x1zoo_f<-zoo(1:10,)
?cbind(xzoo,x1zoo_f)
?? x1zoo_f
1??????? 1
2??????? 2
3??????? 3
4??????? 4
5??????? 5
6??????? 6
7??????? 7
8??????? 8
9??????? 9
10????? 10


#With indices on one
x1zoo_f<-zoo(1:5,1:10)
?? x1zoo_f
1??????? 1
2??????? 2
3??????? 3
4??????? 4
5??????? 5
6??????? 1
7??????? 2
8??????? 3
9??????? 4
10?????? 5

#with a different index.? Here, I get warning message.

?x1zoo_f<-zoo(1:5,c(5,6,8,11,13))
?? x1zoo_f
5??????? 1
6??????? 2
8??????? 3
11?????? 4
13?????? 5
Warning message:
In merge.zoo(..., all = all, fill = fill, suffixes = suffixes, retclass = "zoo",? :
? Index vectors are of different classes: integer numeric


I am using R 2.15.? Don't know if you get warnings or not.
A.K.
#
On Tue, Jul 31, 2012 at 2:40 AM, jpm miao <miaojpm at gmail.com> wrote:
I'm not quite sure what you mean by an array of zoo objects: the
"data" of the zoo object can be a matrix so you can have rows and
columns and all that. Alternatively you can stick the different zoo
objects in a list (as you can with any R object)
Well, Pat Burns himself really -- see, e.g.,
http://www.brown.edu/Departments/Italian_Studies/LD/numbers/04/hollander.html
or less technically, http://www.shmoop.com/inferno/virgil.html.

Michael
#
On Tue, Jul 31, 2012 at 8:23 AM, arun <smartpink111 at yahoo.com> wrote:
Using this will get rid of the warning:

   merge(z, zoo(, numeric(0)))

if you are trying to combine it with a zoo object, z, having a numeric index.