Hi Ben:
I realize that for the OP whether it takes 1/2 second or 1 microsecond to
do what he wants may be irrelevant, but just for fun I thought I'd time the
condense() function you found vs. the compr() function I worked out, which
are similar in their approach.
compr <- function(x, sep ="-")
{
e-use bits and pieces but perhaps for longer examples, not in prototyping
mode, writing a custom function that passes minimally over the data, may be a
better choice.
-Original Message-
From: R-help On Behalf Of Rui Barradas
Sent: Saturday, February 22, 2025 7:36 AM
To: Dennis Fishe
Colleagues
I appreciate the many suggestions for my query about collapsing a sequence to a
shortened string.
I finally arrived at:
format_ranges <- function(x)
{
x <- sort(unique(x))
breaks <- which(diff(x) >
ata, may be a
better choice.
-Original Message-
From: R-help On Behalf Of Rui Barradas
Sent: Saturday, February 22, 2025 7:36 AM
To: Dennis Fisher ; r-help
Subject: Re: [R] Compressing a sequence
Às 00:46 de 22/02/2025, Dennis Fisher escreveu:
> R 4.4.0
> OS X
>
> Col
Well, as I predicted, my initial suggestions were, ... ummm, rather dumb.
Also, Rui's suggestions are probably preferable to the below. However, it
*is* a very simple, bare-boned approach to converting a sequence of
increasing integers to a character representation using interval notation.
The code
Às 00:46 de 22/02/2025, Dennis Fisher escreveu:
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" Comp
And some more from 2013:
https://stackoverflow.com/questions/14868406/collapse-continuous-integer-runs-to-strings-of-ranges
Can be as short as:
condense <- function(x)
unname(tapply(x, c(0, cumsum(diff(x) != 1)), FUN = function(y)
paste(unique(range(y)), collapse = "-")
))
z <- c(1
There are some answers from 2016 here:
https://stackoverflow.com/questions/34636461/collapse-consecutive-runs-of-numbers-to-a-string-of-ranges
On 2025-02-21 7:59 p.m., Steven Ellis wrote:
Hi Dennis,
A quick Claude request:
"using r I have a sequence like:1, 3, 4, 5, 7, 8, 12, 13, 14
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 On Behalf Of Dennis Fisher
Sent: Friday, February 21, 2025 7:46 PM
To: r-help
Subject: [R] Co
Hi Dennis,
A quick Claude request:
"using r I have a sequence like:1, 3, 4, 5, 7, 8, 12, 13, 14, 15,
20I would like to display it as:1, 3-5, 7-8, 12-15, 20"
yielded:
condense_sequence <- function(nums) {
if (length(nums) == 0) return("")
if (length(nums) == 1) return(as.char
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
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)
12 matches
Mail list logo