[R] Compressing a sequence
@vi@e@gross m@iii@g oii gm@ii@com
@vi@e@gross m@iii@g oii gm@ii@com
Sat Feb 22 02:27:54 CET 2025
Dennis,
You are supposedly using a vector containing integers, in order, and want to
find sequences to rewrite compactly based on your example.
What result do you want. An integer vector cannot hold something like 3-5 so
do you want an integer of character stringls like this:
c("1", "3-5", "7-8", "12-15", "20")
Or do you want a single string like: "1, 3-5, 7-8, 12-15, 20"
Or do you want other more exotic things like slices that might not actually
be simpler or shorter.
There may well be such functionality available, perhaps in some package, but
you probably could easily roll your own for the first alternatives. Consider
a function, perhaps recursive, which checks the first item in a vector and
loops as long as it has not reached the end and checks if the next integer
is one more. As soon as it changes, or the end is reached, take the subset
of indices from first to last and bundle them to attach as one unit to what
you have so far. The can mean making a string like first-last to add as one
unit to a growing vector or at the end of a growing string.
If you are doing this recursively, call the function again on a truncated
version of the original input containg the so-far unused parts as something
to attach to what you already have.
This may not be the simple functionality you want so I looked for packages.
This one may be close:
install.packages("denstrip")
library(denstrip)
seqToIntervals(c(1:10, 15:18, 20))
from to
1 1 10
2 15 18
3 20 20
It is a matrix of stop/start and can be converted to your format with a
little work or by using some parts of the source code for yourself and at a
point, substituting in your format. It may take work if you get something
like this:
> apply(ranges, 1, paste, sep="-")
1 2 3
"c(1, 10)" "c(15, 18)" "c(20, 20)"
You might then use something like grep's substitute to replace "c(" and ")"
with nothing and a "," by a "-" and so on.
The package description is here, and there may well be other better ones:
https://search.r-project.org/CRAN/refmans/denstrip/html/seqToIntervals.html
-----Original Message-----
From: R-help <r-help-bounces using r-project.org> On Behalf Of Dennis Fisher
Sent: Friday, February 21, 2025 7:46 PM
To: r-help <r-help using r-project.org>
Subject: [R] Compressing a sequence
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.
More information about the R-help
mailing list