[R] converting a zoo or an xts to a data frame

Gabor Grothendieck ggrothendieck at gmail.com
Tue Apr 20 05:15:49 CEST 2010

If z is a zoo series as.data.frame(z) converts it to a data frame.
If that is not what you are asking please clarify.  Also read the last
line to every message to r-help asking for complete self contained
code.  Yours has undefined variables.

Also what is the purpose of the code?  Normally its unnecessary to
have a ts class column in a data frame.

Note that get.hist.quote can output ts series directly if you use the
retclass= argument.

There are facilities in quantmod for dealing with stock price data
that you might want to look at.

On Mon, Apr 19, 2010 at 10:31 PM, Erin Hodgess <erinm.hodgess at gmail.com> wrote:
> Dear R People:
> I have the following code that I use to convert a monthly zoo object
> to a data.frame, and it works perfectly:
>  library(tseries)
>  z <- get.hist.quote(instrument=inst1, start=start1,end=end1,
>                     quote=quot1,comp = "m")
> y <- as.ts(aggregate(z, as.yearmon, tail, 1))
> y.df <- data.frame(y=y,time=time(y))
> y.df$x <- ts(y.df[,1])
> tsp(y.df$x) <- tsp(y.df[,2])
> names(y.df) <- c("data","time","ts")
> z.df <- data.frame(ts=y.df$ts)
> Fair enough.  Now I would like to take a daily or weekly zoo and
> convert that to a data frame.  It will probably follow a similar
> pattern to what I have here, but I'm not sure how to do this.
> Any help would be much appreciated.
> thanks,
> Erin
> --
> Erin Hodgess
> Associate Professor
> Department of Computer and Mathematical Sciences
> University of Houston - Downtown
> mailto: erinm.hodgess at gmail.com
> ______________________________________________
> 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.

More information about the R-help mailing list