Skip to content
Prev 55097 / 63424 Next

extendrange(): suggested improvement

> Hi,
    > I often need to extend the plot range to the right, but not to the
    > left (for example: not below 0 so that log = "x" still works...). This
    > could be a handy improvement of extendrange():
Yes, you are right and it's not the first time I've heard/seen
this wish.
Thank you for the suggestion!

I'd go for the more elegant (faster, at least in default case?) 
version


extendrange <- function(x, r = range(x, na.rm = TRUE), f = 0.05)
{
    ## Purpose: extend a range by a factor 'f' - on each side
    if(!missing(r) && length(r) != 2)
        stop("'r' must be a \"range\", hence of length 2")
    f <- if(length(f) == 1L) c(-f,f) else c(-f[1L], f[2L])
    r + f * diff(r)
}

PS:
/* I hope the tidy faction will at some time be convinced that
  using  if()  as a *function* in R is elegant and *the* R-way: 
  R is as functional a language as possible and hereby differs
  from C and similar languages ! 
*/

Martin Maechler
ETH Zurich