Skip to content
Prev 261633 / 398502 Next

Recode numbers

Hi:

Here's another option:

rep(b, rle(a)$lengths)
[1] TRUE

rle(a)$lengths computes a table of the number of consecutive repeats
of a number (or run lengths). It will have the same length as b in
this case. Using rep() with the table of lengths as repetitions gets
the desired result:
[1]  1  5  8  8  9  9 14 20  3 10 10 12  6 16  7 11 13 13 17 18  2  4 15 19

HTH,
Dennis
On Wed, Jun 1, 2011 at 10:19 AM, Lisa <lisajca at gmail.com> wrote: