[R] multiple return values
sundar.dorai-raj at pdf.com
Mon Mar 20 19:49:50 CET 2006
Gavin Simpson wrote:
> On Mon, 2006-03-20 at 13:06 -0500, Sam Steingold wrote:
>>I am pretty sure I saw this mentioned in
>>but I cannot recall how it was called. sorry...
>>how do I return (and accept) multiple return values from a function?
>>e.g., in lisp (multiple-value-bind (f r) (floor 10 3) (list f r))
>>will return list (3 1).
> Generally in a list, like this:
> foo <- function(x)
> dat <- runif(x)
> m <- mean(dat)
> ran <- range(dat)
> retval <- list(mean = m, range = ran)
> ## or in one step:
> ## return(list(mean = m, range = ran))
> bar <- foo(100)
Note that using "return" as you have is not necessary (and some would
say, not good style -- but I digress...).
foo <- function(x)
dat <- runif(x)
m <- mean(dat)
ran <- range(dat)
list(mean = m, range = ran)
This is mentioned in ?return.
More information about the R-help