# [R] R Novice Question

Prof Brian D Ripley ripley at stats.ox.ac.uk
Wed Apr 18 07:52:31 CEST 2001

```On Wed, 18 Apr 2001 MichaelNielsen at synergy.com.au wrote:

> Please forgive the relative lack of sophistication here -- I'm trying to
> learn R, remove the rust from my basic training in statistics, AND produce
> a useful result.
>
> I have two vectors of equal length,  A and B, with a one-to-one
> correspondence between the elements (ie. A[i] corresponds to B[i]) which
> are provided by an SQL server upon which I can run any SQL statement.
>
> I would like to partition A (like breaks in a histogram) into some number
> of equal-size partitions, and then calculate and plot the mean value of
> the B's that correspond to the A's in each partition (or, more generally,
> produce some descriptive statistics for each set of B's, and potentially
> try to draw some inferences about the B's in each partition).
>
> I can imagine how to do this in Perl by writing a loop, but I suspect
> there may be a more natural way to do it in R, and would be grateful for
> any suggestions.

Use something like

tapply(B, cut(A, 5), mean)

cut() gives 5 (roughly) equal-sized pieces, and tapply partitions on the
values of the cut and applies the function.

For more complicated situations you might like to look at by().

--
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 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

```