[R] How to assign multiple return values

From: Tribo Laboy <tribolaboy_at_gmail.com>
Date: Mon, 24 Mar 2008 18:09:11 +0900


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?

http://thread.gmane.org/gmane.comp.lang.r.general/21223/focus=21226 http://thread.gmane.org/gmane.comp.lang.r.general/42875/focus=42876

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...

Regards,

TL



R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Mon 24 Mar 2008 - 09:17:19 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 https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive