Skip to content

seq range argument

4 messages · Lorenz, David, Berry Boessenkool, Ista Zahn

#
Hello dear developers,

I find myself often having the result of "range" oder "extendrange", which I want to create a sequence with.
But "seq" needs two seperate arguments "from" and "two".
Could an argument "range" be added?

Otherwise I will have to create an object with the range (may come from a longer calculation), index twice from it and remove it again - kind of an unnecessary bunch of code, I think.

Here's an example:

# What I currently do:
D_orig <- rnorm(20, sd=30)
D_range <- extendrange(D_orig, f=0.1)
Ijustneed <- seq(D_range[1], D_range[2], len=100)
rm(D_range)

# what I'd like to have instead:
D_orig <- rnorm(20, sd=30)
Ijustneed <- seq(range=extendrange(D_orig, f=0.1), len=100)

regards,
Berry

-------------------------------------
Berry Boessenkool
Potsdam, Germany
-------------------------------------
#
Thanks Dave!
I settled on something similar, but more general, for my function collection package.
I just thought this could be wanted by more people and thus interesting for base::seq.

In case anyone is interested in my solution, keep reading.

Berry


# Add a range argument to seq

seqr <- function(from=1, to=1, range, ...)
{
# Input checking:
if(!is.vector(range)) stop("'range' must be a vector.")
if(!is.numeric(range)) stop("'range' must be numeric.")
# only set from and to if range is given as input:
if(!missing(range)) 
? {
? from <- range[1]???? # first
? to <- tail(range,1)? # and last value
? if(length(range)>2L)
???? {
???? from <- min(range, finite=TRUE) # min
???? to?? <- max(range, finite=TRUE) # and max
???? }
? }
# now call seq with from and to (obtained from range)
seq(from=from, to=to, ...)
}


# Examples
m <- c(41, 12, 38, 29, 50, 39, 22)
seqr(range=extendrange(m, f=0.1), len=5)
# Takes min and max of range if thae vector has more than two elements. 
seqr(range=m, by=3)


-----------


From: lorenz at usgs.gov
Date: Mon, 3 Feb 2014 08:00:41 -0600
Subject: Re: [Rd] seq range argument
To: berryboessenkool at hotmail.com
CC: r-devel at r-project.org

Berry,? It sounds like you just need a little helper function like this:
ser <- function(x, len=100, f=0.1) {? ?dr <- extendrange(x, f=f)? ?return(seq(dr[1L], dr[2L], length.out=len))

?}
I called it ser, short for sequence extended range. Use it thusly:
Ijustneed <- ser(D_orig)
? Hope this helps. I use and create little help functions like this all the time. Even extendrange could be considered a helper function as it is only a couple of lines long.

Dave
#
This is slightly more verbose, but perhaps

do.call("seq", as.list(c(extendrange(D_orig, f=0.1), len=100)))

Best,
Ista
On Mon, Feb 3, 2014 at 9:00 AM, Lorenz, David <lorenz at usgs.gov> wrote: