Dear R-helpers, It seems to me that a character zoo cannot be coerced to a numeric zoo. Below is a minimal example. Can someone tell me what I have done wrong?> z<-zoo(1:4,order.by=1:4) > coredata(z)<-as.character(coredata(z)) > str(z)‘zoo’ series from 1 to 4 Data: chr [1:4] "1" "2" "3" "4" Index: int [1:4] 1 2 3 4> coredata(z)<-as.numeric(coredata(z)) > str(z)‘zoo’ series from 1 to 4 Data: chr [1:4] "1" "2" "3" "4" Index: int [1:4] 1 2 3 4 Many thanks, Ashim [[alternative HTML version deleted]]
Yes, once made into a character zoo, the core data is marked to be of mode "character" and most attempts to modify involve implicit coercion to that mode. The following however works: library(zoo) z <- zoo(1:4, order.by=1:4) str(z) z.Str <- z coredata(z.Str) <- as.character(coredata(z)) str(z.Str) z.Num <- z.Str mode(z.Num) <- "numeric" str(z.Num) However, I prefer to use this sort of line of code: z.Num <- zoo(as.double(z.Str), index(z.Str)) finding it a little more transparent. Hope this helps, Michael Weylandt On Tue, Sep 27, 2011 at 5:56 AM, Ashim Kapoor <ashimkapoor@gmail.com> wrote:> Dear R-helpers, > > It seems to me that a character zoo cannot be coerced to a numeric zoo. > Below is a minimal example. Can someone tell me what I have done wrong? > > > z<-zoo(1:4,order.by=1:4) > > coredata(z)<-as.character(coredata(z)) > > str(z) > ‘zoo’ series from 1 to 4 > Data: chr [1:4] "1" "2" "3" "4" > Index: int [1:4] 1 2 3 4 > > coredata(z)<-as.numeric(coredata(z)) > > str(z) > ‘zoo’ series from 1 to 4 > Data: chr [1:4] "1" "2" "3" "4" > Index: int [1:4] 1 2 3 4 > > > Many thanks, > Ashim > > [[alternative HTML version deleted]] > > > ______________________________________________ > R-help@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. > >[[alternative HTML version deleted]]
On Tue, Sep 27, 2011 at 5:56 AM, Ashim Kapoor <ashimkapoor at gmail.com> wrote:> Dear R-helpers, > > It seems to me that a character zoo cannot be coerced to a numeric zoo. > Below is a minimal example. Can someone tell me what I have done wrong? > >> z<-zoo(1:4,order.by=1:4) >> coredata(z)<-as.character(coredata(z)) >> str(z) > ?zoo? series from 1 to 4 > ?Data: chr [1:4] "1" "2" "3" "4" > ?Index: ?int [1:4] 1 2 3 4 >> coredata(z)<-as.numeric(coredata(z)) >> str(z) > ?zoo? series from 1 to 4 > ?Data: chr [1:4] "1" "2" "3" "4" > ?Index: ?int [1:4] 1 2 3 4 > >See ?zoo where it says that the zoo object may be "a numeric vector, matrix or a factor". Thus character is not supported although I suspect that a number of operations continue to work anyways -- although evidently not that one. This seems to result in a numeric zoo object: aggregate(z, identity, as.numeric) although these sorts of computations with zoo objects that strictly speaking are not legal are, of course, not officially supported. -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com