[R] Grouping data via an index

Tobin, Jared TobinJR at DFO-MPO.GC.CA
Mon Jan 28 15:31:17 CET 2008


Hello r-help,

I have a lengthy vector of data (with values anywhere from 1-200), and
another index vector of 'groups' representing values 0-2, 3-5, 6-8, ...
of length 67.  The index vector has the structure (1, 4, 7, ... , 196,
199), where each value is the midpoint of each respective group.

I'm trying to convert the data vector such that values falling into each
group are changed to the 'number' of that group.  That is, the position
of the midpoint of that group in the index vector.  For example, 4 or 5
would become a 2; 6 would become 3; 9 or 10 would become 4, and so on.

Haven't had any success thus far -- does anyone know of a simple method
offhand?

I've started by converting the data vector to a vector of the midpoints
of each group, via 
> round(data.vector/3)*3 + 1

But haven't been able to accomplish much past that.  I'm guessing it can
be accomplished via a simple loop or otherwise.

Thanks,

--

jared tobin, student research assistant
fisheries and oceans canada
tobinjr at dfo-mpo.gc.ca



More information about the R-help mailing list