[R] Conversion of zoo object (with POSIXct-attributes) to dataframe

Achim Zeileis Achim.Zeileis at wu-wien.ac.at
Thu Aug 23 15:31:36 CEST 2007


On Thu, 23 Aug 2007 mgilgen at swissonline.ch wrote:

> i'm trying to convert a zoo object of the following specifications:
>
> > str(z)
>  atomic [1:15642] 0 0 0 0 0 0 0 0 0 0 ...
>  - attr(*, "index")='POSIXct', format: chr [1:15642] "2004-09-01 02:00:00" "2004-09-01 03:00:00" "2004-09-01 04:00:00" "2004-09-01 05:00:00" ...
>  - attr(*, "frequency")= num 0.000278

(Side note 1: The frequency looks suspicious...you probably want a "zoo"
rather than a "zooreg" object.)

> to convert into a dataframe. the atomic object (or what it is...) is

(Side note 2: a vector for univariate zoo series, a matrix for
multivariate zoo series.)

> convertet correctly, but the time attribute in the POSIXct format is
> replaced by an index number
>
> how can i get my data with the corresponding date/time as a second column?

Just add it afterwards, e.g.,
  z <- zoo(cbind(a = 1:5, b = 2:6), as.POSIXct(as.Date("2007-01-01") + 0:4))
  d <- as.data.frame(z)
  d$Date <- time(z)

(Side note 3: The reason for not including the date is that we wanted the
data to be as similar as possible in the zoo series and the data frame,
e.g., for
  lm(a ~ ., data = as.data.frame(z))
)

hth,
Z



More information about the R-help mailing list