[R] Compressing a sequence

Bert Gunter bgunter@4567 @end|ng |rom gm@||@com
Sat Feb 22 02:09:47 CET 2025


Cute exercise!

"Simple" is in the eyes of the beholder, of course. There is probably a
package out there that can do this in a trice .
But my first thought -- so caveat emptor!, as my thoughts, first or last,
are often not so, um... thoughtful -- is to diff() the sequence (as
numerics) -- so that differences of 1 appears as identical -- and then make
use of rle(). I think that one should be able to tweak this appropriately
to make it work, but I leave the details to you (and to me as something to
keep the little gray cells from atrophying). Note that somewhere along the
line, you'll have to convert numerics to character, as "-" is obviously not
going to cut it as a numeric. :-)

Cheers,
Bert
Bert

"An educated person is one who can entertain new ideas, entertain others,
and entertain herself."



On Fri, Feb 21, 2025 at 4:46 PM Dennis Fisher <fisher using plessthan.com> wrote:

> R 4.4.0
> OS X
>
> Colleagues
>
> I have a sequence like:
>         1, 3, 4, 5, 7, 8, 12, 13, 14, 15, 20
>
> I would like to display it as:
>         1, 3-5, 7-8, 12-15, 20
>
> Any simple ways to accomplish this?
>
> Dennis
>
>
> Dennis Fisher MD
> P < (The "P Less Than" Company)
> Phone / Fax: 1-866-PLessThan (1-866-753-7784)
> www.PLessThan.com
>
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> https://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list