views:

48

answers:

1

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

+1  A: 

I got several answers:

library(zoo) 
library(chron)

#1
zz <- z <- zoo(1:10, chron(0:9/5)) 
zz[] <- ave(coredata(z), as.Date(time(z)), FUN = function(x) head(x, 1)) 
cbind(z, zz)


#2
z <- zoo(1:10, chron(0:9/5)) 
z.ag <- aggregate(z, as.Date, head, 1) 
na.locf(z.ag, xout = time(z)) 


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

cheers

You should format this as 'code' by highlighting and clicking the little 010101 button.
Dirk Eddelbuettel
And also provide a link to the original source.
G. Grothendieck