[R] multiple return values

Gavin Simpson gavin.simpson at ucl.ac.uk
Mon Mar 20 19:38:16 CET 2006


On Mon, 2006-03-20 at 13:06 -0500, Sam Steingold wrote:
> I am pretty sure I saw this mentioned in
> http://cran.r-project.org/doc/manuals/R-intro.html
> 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).
> 
> thanks!
> 

Generally in a list, like this:

foo <- function(x)
  {
    dat <- runif(x)
    m <- mean(dat)
    ran <- range(dat)
    retval <- list(mean = m, range = ran)
    return(retval)
    ## or in one step:
    ## return(list(mean = m, range = ran))
  }

foo(100)

bar <- foo(100)
bar

HTH

G

-- 
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
Gavin Simpson                     [T] +44 (0)20 7679 5522
ENSIS Research Fellow             [F] +44 (0)20 7679 7565
ENSIS Ltd. & ECRC                 [E] gavin.simpsonATNOSPAMucl.ac.uk
UCL Department of Geography       [W] http://www.ucl.ac.uk/~ucfagls/cv/
26 Bedford Way                    [W] http://www.ucl.ac.uk/~ucfagls/
London.  WC1H 0AP.
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%




More information about the R-help mailing list