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! -- Sam Steingold (http://www.podval.org/~sds) on Fedora Core release 4 (Stentz) http://truepeace.org http://www.dhimmi.com http://www.mideasttruth.com http://www.palestinefacts.org http://www.jihadwatch.org http://www.camera.org Illiterate? Write today, for free help!
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. %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
Gavin Simpson wrote:> 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 >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) } foo(100) This is mentioned in ?return. HTH, --sundar