[R] Repeat the first day data through all the day. Zoo

Gabor Grothendieck ggrothendieck at gmail.com
Wed Aug 25 14:39:14 CEST 2010


On Wed, Aug 25, 2010 at 7:43 AM, skan <juanpide at gmail.com> wrote:
>
>  down vote  favorite
>
>
> Hello
>
> I have a zoo series. It lasts 10 years and its frequency is 15min.
>
> I'd like to get a new zoo series (or vector) with the same number of
> elements, whith each element equal to the first element of the day. That's,
> The first element everyday is repeated throughout the wole day.
>
> This is not same as aggregate(originalseries,as.Date,head,1) because this
> gives a vector with just one element for each day.
>
> cheers
>

Here are a few more solutions too:

library(zoo)
library(chron)
z <- zoo(1:10, chron(0:9/5))

# aggregate / na.locf
z.ag <- aggregate(z, as.Date, head, 1)
na.locf(z.ag, xout = time(z))

# duplicated / na.locf
z.na <- ifelse.zoo(!duplicated(as.Date(time(z))), z, NA)
na.locf(z.na)

# ave - as before
zz <- z
zz[] <- ave(coredata(z), as.Date(time(z)), FUN = function(x) head(x, 1))
zz



More information about the R-help mailing list