[R] Collapsing data.frame to its or xts

Joshua Ulrich josh.m.ulrich at gmail.com
Thu Jun 5 11:09:26 CEST 2014


On Wed, Jun 4, 2014 at 3:52 PM, Costas Vorlow <costas.vorlow at gmail.com> wrote:
> Hello,
>
> I have a data.frame of a time series sampled every 15 minutes. Also the
> data are in reverse order (recent to older). It looks like the following:
>
<snip lots of data>

> Each row has 96 elements (excluding the date column), i.e., there are 96
> columns of 15 minute intervals from 0:00 time to 23:45 apart from the 1st
> column that has the dates in dd/mm/yy format. The data start on the 30th of
> June, 2011, 0:00.
>
> I made a rudimentary function to read the data.frame from its csv file,
> reverse it and create a single time series (xts) that has a 15min frequency.
>
> preparehfdata<-function(x,ISOdate=ISOdatetime(2011,06,29,23,45,0))
> {
>   mydata<-x[nrow(x):1,]
>   temp<-(as.matrix(mydata))
>   temp2<-temp[,-1]
>   temp3<-as.numeric(apply(temp2, MARGIN=1, FUN=as.vector))
>   hoursminutes <- ISOdate + seq(0:(length(temp3)-1))*15*60
>   temp4<-xts(temp3, order.by=hoursminutes)
>   return(temp4)
> }
>
> which works fine except from the part that the data.frame is missing some
> days and my time alignment overshoots. I.e., although there should have
> been observations (96 15-minute prices) for every day since June 30th 2011,
> some days are missing and some days are repeated (see rows 3, 4 and 5 for
> example above) in the data.frame, with the same prices...
>
> Is there an easier way to omit copies of same date data and account for the
> missing days when collapsing the dataframe to a single time series (xts or
> zoo)?
>
> Preferably I would like to avoid treating this as an irrelevantly spaced
> time series (its) if possible...
>
> Many thanks in advance for your input,
>
> Best,
> Costas
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

It will be a lot easier, and therefore more likely, for others to help
you if you follow the instructions in the footer.  Here are some
suggestions of how to create a *minimal*, reproducible example:
http://stackoverflow.com/q/5963269/271616

Best,
--
Joshua Ulrich  |  about.me/joshuaulrich
FOSS Trading  |  www.fosstrading.com



More information about the R-help mailing list