# [R] How to "bin"/average" time points?

Niels Richard Hansen Niels.R.Hansen+lists at math.ku.dk
Fri Nov 26 03:21:57 CET 2010

```Hi Kevin

Here is one way:

yourData <- c(0.730, 0.732, 0.743, 0.757,0.781, 0.731,
0.830, 0.832, 0.843, 0.857, 0.881, 0.831)

nrGroups <- 2
lengthGroups <- 6

tapply(yourData, factor(rep(c(1,nrGroups), each = lengthGroups)), mean)

and you will have to adjust the number of groups and if necessary the
length of the groups to your needs. I am assuming that all groups are
of the same length.

Do you really want to average the time points? In that case you might
want to consider the data structures for representing dates and times,
see e.g.

?POSIXct

or converting the times to minutes, say.

- Niels

On 25/11/10 12.49, DonDolowy wrote:
>
> Dear all,
>
> I am pretty new to R only having an introduction course, so please bare with
> me. I am doing my PhD at The Max Planck Institute of Immunobiology where I
> am analyzing some calorimetry data from some mice.
> I have a spreadsheet consisting of measurements of the respiratory exchange
> rate at different time points measured every 9 minutes over some days.
> My goal is "bin"/average the time points of each hour to only one
> measurements.
>
> E.g.
> [Time] - [Measurement]
> 12.09 - 0.730
> 12.18 - 0.732
> 12.27 - 0.743
> 12.36 - 0.757
> 12.45 - 0.781
> 12.54 - 0.731
> -->  should be averaged to fx one time point and one value, fx:
> 12.30 - [average of the six measurements]
>
> I know how to average the measurements in a whole column but how to average
> every six measurements automatically and also how to average every six time
> points and make a new sheet consisting of these data?
>
> I hope you guys are able to help, since we are really stuck here. I can of
> course do it manually but with>8000 measurements it will take lots of time.
>
> Thank you very much.
>
> Best regards,
> Kevin Dalgaard

```

More information about the R-help mailing list