[R] x y averaging
Prof Brian Ripley
ripley at stats.ox.ac.uk
Wed Jun 7 09:14:18 CEST 2006
It's confusing to have time values that are different but `match', so
first you want to reduce your times to hh:mm (e.g. with substr).
Then you can cbind() the data frames, and use tapply() to do the
averaging. E.g.
d1 <- read.table("foo1")
d2 <- read.table("foo2")
d <- cbind(d1, d2)
d$time <- factor(substr(as.character(d$x), 1, 5))
tapply(d$y, d$time, mean)
14:56 14:62 14:68 15:59 16:05 16:11 16:17 16:23 16:33 16:39
0.3250 0.1110 0.2140 0.2575 0.2545 0.3040 0.2550 0.2630 0.2630 0.3000
There are other ways, e.g.
aggregate(d["y"], list(d$time), mean)
or via ave().
On Tue, 6 Jun 2006, Randy Zelick wrote:
> Hello,
>
> I am trying to average a number of data sets where the x vector contains
> times and the Y data are instrument readings. The vectors all have
> different numbers of values, but many X time values match. For example:
>
> A fragment of the first data set:
>
> x vect value
> 14:56:10 0.325
> 14:62:11 0.111
> 14:68:11 0.214
> .
> .
> .
> this can go on for 100's of values, spaced by 6 minutes, but there can be
> gaps too, like:
>
> 16:05:18 0.245
> 16:11:09 0.266
> 16:17:05 0.271
> 16:33:00 0.304
> 16:39:05 0.300
>
>
> A fragment of the second data set:
>
> 15:59:08 0.255
> 16:05:44 0.281
> 16:11:25 0.249
> 16:17:39 0.238
> 16:23:51 0.288
>
>
> ...and the result I am looking for is a new vector that looks like this:
>
> 14:56:10 0.325
> 14:62:11 0.111
> 14:68:11 0.214
> .
> .
> .
> 15:59:08 0.255
> 16:05:18 0.2630 * matches, so average
> 16:11:09 0.2575 * matches, so average
> 16:17:05 0.2545 * matches, so average
> 16:33:00 0.304
> 16:39:05 0.300
>
>
> The times values in the new X vector are properly interleaved taking data
> across the datasets, but when there is a match the Y values are averaged.
> Note that the seconds don't matter.
>
> I am using R Version 2.2.1 on a PC
>
> Thanks for thinking about it,
>
> =Randy=
>
> R. Zelick email: zelickr at pdx.edu
> Department of Biology voice: 503-725-3086
> Portland State University fax: 503-725-3888
>
> mailing:
> P.O. Box 751
> Portland, OR 97207
>
> shipping:
> 1719 SW 10th Ave, Room 246
> Portland, OR 97201
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>
--
Brian D. Ripley, ripley at stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UK Fax: +44 1865 272595
More information about the R-help
mailing list