[R] Does this exist: diff(range(VAR, na.rm=T))

Bert Gunter bgunter@4567 @end|ng |rom gm@||@com
Wed Jun 25 22:15:15 CEST 2025


Just wanted to point out that:

1. range() is a generic function and the default version works for
character objects also:

> range("Joe", "Q", "Public")
[1] "Joe" "Q"

## but

> diff(range("Joe", "Q", "Public"))
Error in r[i1] - r[-length(r):-(length(r) - lag + 1L)] :
  non-numeric argument to binary operator

Similarly, the max - min version won't work:

> x <- c("Joe", "Q", "Public")
> max(x)
[1] "Q"
> min(x)
[1] "Joe"

> max(x) - min(x)
Error in max(x) - min(x) : non-numeric argument to binary operator

So *exactly* how/if to implement a span() type function even for the
default version of range() seems nontrivial. And should it be generic? etc.

I do not mean to start a discussion on these issues -- I only wanted to
point out that sometimes even what seem to be "simple" issues may have
complications.

Cheers,
Bert


On Wed, Jun 25, 2025 at 12:47 PM Rui Barradas <ruipbarradas using sapo.pt> wrote:

> Hello,
>
> I know that this is not what you want but cases like this are when
> personal packages make life easier.
> They don't have to be contributed packages, they are more of a
> collection of personal habits.
>
>
>
> span <- function(x, na.rm = FALSE) diff(range(x, na.rm = na.rm))
>
>
>
> min, max, range and others default to na.rm = FALSE. Change at will.
>
> Hope this helps,
>
> Rui Barradas
>
>
>
> Às 20:34 de 25/06/2025, Dennis Fisher escreveu:
> > you are missing the point:
> >       span(…)
> > requires less typing than
> >       diff(range(….))
> >
> > just like
> >       paste0
> > simplifies use of the paste command
> >
> >
> > Dennis Fisher MD
> > P < (The "P Less Than" Company)
> > Phone: 1-415-307-4791
> > www.PLessThan.com
> >
> >> On Jun 25, 2025, at 12:32 PM, Ebert,Timothy Aaron <tebert using ufl.edu>
> wrote:
> >>
> >> I do not see the problem.
> >> In base R
> >> Min <- min(var, na.rm=T)
> >> Max <- max(var, na.rm=T)
> >> Span <- c(Min, Max)
> >>
> >> Is the same output as
> >> Span <- range(var, na.rm=T)
> >>
> >> diff(range(var, na.rm=T)) returns Max - Min
> >>
> >> range() returns a vector with two values, the minimum and the maximum.
> >> What should the function "span" do that is not already done?
> >>
> >> Tim
> >>
> >> -----Original Message-----
> >> From: R-help <r-help-bounces using r-project.org <mailto:
> r-help-bounces using r-project.org>> On Behalf Of Dennis Fisher
> >> Sent: Wednesday, June 25, 2025 2:11 PM
> >> To: r-help <r-help using r-project.org <mailto:r-help using r-project.org>>
> >> Subject: [R] Does this exist: diff(range(VAR, na.rm=T))
> >>
> >> [External Email]
> >>
> >> Version 4.4.2
> >> OS X
> >>
> >> Colleagues
> >>
> >> I often need to determine the span of the x-variable in a graphic.
> >>
> >> This is easy to determine with:
> >>         diff(range(VAR, na.rm=T))
> >>
> >> I am not aware of any function in base R that accomplishes this and
> "span" is not taken.
> >>
> >> Would it be possible to add such a function?  Not a major addition but
> often useful.
> >>
> >> Dennis
> >>
> >> Dennis Fisher MD
> >> P < (The "P Less Than" Company)
> >> Phone: 1-415-307-4791
> >> http://www.plessthan.com/
> >>
> >>
> >>         [[alternative HTML version deleted]]
> >>
> >> ______________________________________________
> >> R-help using r-project.org <mailto: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]]
> >
> > ______________________________________________
> > 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.
>
>
> --
> Este e-mail foi analisado pelo software antivírus AVG para verificar a
> presença de vírus.
> www.avg.com
>
> ______________________________________________
> 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