Re: [R] How to assign multiple return values

From: Duncan Murdoch <>
Date: Mon, 24 Mar 2008 05:48:09 -0400

On 24/03/2008 5:09 AM, Tribo Laboy wrote:
> Hi,
> I am moving from MATLAB, where one can easily assign a number of
> output values from a function like this:
> [x,y] = myfun(a,b)
> Then variables x and y can be directly used in the caller workspace.
> I understand that R functions return a single argument, which could be
> a list. This in a way makes it possible to return multiple values with
> a single function call, but accessing the list variables is a little
> bit awkward.
> mylist <-myfun(a,b) {
> return(list(x = a, y = b))
> }
> x = mylist$x; y = mylist$y
> I found some discussion going back to 2004. Is there anything new to
> add to what was said at the time?

I don't think so.

> While at it, somewhere in the "Introduction to R", I think, I read
> that use of "return" is considered a bad practice. Why is that? I
> thought making clear what is returned by the function is a positive
> thing...

Where did you see that? It would be good advice if it said not to hide many calls to return() deep within a long function. I'd be neutral about it if it said not to bother with return() in a very short function. In intermediate cases, I think using return() is good practice.

Duncan Murdoch mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code. Received on Mon 24 Mar 2008 - 09:55:35 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 24 Mar 2008 - 10:30:23 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive