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

Achim Zeileis Achim.Zeileis at wu-wien.ac.at
Thu Aug 23 17:15:09 CEST 2007


On Thu, 23 Aug 2007, Marc Gilgen wrote:

> Thanks for the inputs, but it's not exactly what I want...

You did not provide sufficient information. In particular, you still do
not provide a small reproducible example.

> My problem is, that my timeseries (precipitation) contain lots of NA-values
>
> and when I apply the code:
>
> z<- data.frame(a=p1$time.date, precip=p1$m1)

Why do you store this as data frame and not as a zoo series? If you had a
zoo series, you could use...

> #gives a dataframe with precipitation and time (2004-01-01 02:12:00)
> z<- z[complete.cases(z),]
> m1.h<-tapply(z$precip, as.POSIXct(trunc(z$a, 'day')), sum)

...the aggregate() method for zoo series here.

> #gives me the daily sum values of this serie (I need to redo this also for
> hourly values)
> m1.h<-as.data.frame(m1.h)
>
> to get daily summations, the NAs are out and the time series is not
> continous, therefore I need the time/dates corresponding to the measurement
> back in the DATA-FRAME!
>
> Can I do this, or is the way with tapply/trunc wrong to get hourly/daily
> summation of my measurements (which are originally in 10 and 15 min period
> (randomly).

I don't understand why you want to keep going back and forth between zoo
and data.frame. I would just keep it in zoo and use the corresponding
mehtods. See ?aggregate.zoo for example.

hth,
Z

> really, really thanks a lot...
>
> Marc
>
> 2007/8/23, Achim Zeileis <Achim.Zeileis at wu-wien.ac.at>:
> >
> > 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
> >
> >
> >
>
>
> --
> ***
> Marc Gilgen
> Plattenstrasse 21
> 8032 Zürich
> +41 78 686 06 49
> marc.gilgen at gmail.com
>



More information about the R-help mailing list