1
votes

Given a data frame:

'data.frame':   2122 obs. of  2 variables:
 $ hour : POSIXlt, format: "2015-01-01 00:00:00" "2015-01-01 01:00:00" ...
 $ z    : int  756 693 675 369 224 487 595 705 573 713 ...

where hour is incremented by one hour, how do I create a time series?

I want a single series, 24 records per day.

I know I must be missing something trivial. I tried as.ts(f$z, start=c(hd$hour[1],1), end=c(tail(f$hour,1),1))). Nope.

Thanks.

PS. The next step would be to aggregate the series into into a daily one...

1
What is hd in hd$hour[1]? you mean 'f'?Soheil

1 Answers

1
votes
library(ggplot2)
f<-data.frame(hour=seq(as.POSIXct("2015-01-01 00:00:00"),
                   as.POSIXct("2015-01-02 23:00:00"),by="hour"),
              z=seq(1,48,by=1))

f$hour<- format(f$hour, format="%Y/%m/%d %H")

ggplot(f, aes(x=hour, y=z))+
  geom_point(col="brown1",size=1)

For aggregate, you can choose daily format for your time:

f$hour<- format(f$hour, format="%Y/%m/%d")
result<-aggregate(.~hour,f, sum)