[R] Averaging half hourly data to hourly

Joshua Ulrich josh.m.ulrich at gmail.com
Mon Jun 28 16:02:06 CEST 2010


Hi Jenny,

> Date: Thu, 24 Jun 2010 09:45:10 +0100
> From: Jennifer Wright <J.Wright-11 at sms.ed.ac.uk>
> To: r-help at r-project.org
> Subject: [R] Averaging half hourly data to hourly
> Message-ID: <4C231B16.1080604 at sms.ed.ac.uk>
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>
> Hi all,
>
> I have some time-series data in half hourly time steps and I want to be
> able to either average or sum the two half hours together into hours.
> Any help greatly appreciated.
>
> Thanks,
>
> Jenny
>
>
> --
> The University of Edinburgh is a charitable body, registered in
> Scotland, with registration number SC005336.
>
>

See period.apply and endpoints in the xts package:

> require(xts)
> (x <- .xts(1:10,(1:10)*1800))
                    [,1]
1969-12-31 18:30:00    1
1969-12-31 19:00:00    2
1969-12-31 19:30:00    3
1969-12-31 20:00:00    4
1969-12-31 20:30:00    5
1969-12-31 21:00:00    6
1969-12-31 21:30:00    7
1969-12-31 22:00:00    8
1969-12-31 22:30:00    9
1969-12-31 23:00:00   10
> period.apply(x, endpoints(x,"hours"), sum)
                    [,1]
1969-12-31 18:30:00    1
1969-12-31 19:30:00    5
1969-12-31 20:30:00    9
1969-12-31 21:30:00   13
1969-12-31 22:30:00   17
1969-12-31 23:00:00   10
> period.apply(x, endpoints(x,"hours"), mean)
                    [,1]
1969-12-31 18:30:00  1.0
1969-12-31 19:30:00  2.5
1969-12-31 20:30:00  4.5
1969-12-31 21:30:00  6.5
1969-12-31 22:30:00  8.5
1969-12-31 23:00:00 10.0


Best,
--
Joshua Ulrich
FOSS Trading: www.fosstrading.com



More information about the R-help mailing list