[R] multi-argument returns

Steve Lianoglou mailinglist.honeypot at gmail.com
Wed Feb 17 22:59:22 CET 2010


On Wed, Feb 17, 2010 at 4:09 PM, Randall Wrong <randall.wrong at gmail.com> wrote:
> Dear R users,
> I have multi-argument returns in a function and I am warned by the program
> they are deprecated.
> I have found this in the R-help archives :
> http://tolstoy.newcastle.edu.au/R/help/01c/0319.html
> http://tolstoy.newcastle.edu.au/R/help/01c/0356.html
> Since I am not too good at programming, the list solution seems the better
> one for me. It is also the one advocated by Kevin Murphy.
> So rather than writing return(x,y,z), I should write at the end of my
> function :
> g=function() {
>    #...
>    result=list(x,y,z)
>    return(result)
> }
> Is that correct ?

FYI, in R the last line of a function is its return value, so you
could simply do:

g <- function() {
  list(x=x, y=y, z=z)

> Then shoud l use g[1] or g[[1]] ?

Just to avoid a mistake or ambiguity here, I wouldn't use "g" as a
variable because you are using that as your function name.

So after defining the function `g`, you could do:

myvalue <- g()

Then you would use: myvalue[[1]]

myvalue[1] would return you a list that has one element in it (the
first one), where as using myvalue[[1]] just gives you the first
element of the list.

Using "named" arguments when constructing your list as I did (eg.
list(x=x, ...)), you can then also do:


Hope that helps,
Steve Lianoglou
Graduate Student: Computational Systems Biology
 | Memorial Sloan-Kettering Cancer Center
 | Weill Medical College of Cornell University
Contact Info: http://cbio.mskcc.org/~lianos/contact

More information about the R-help mailing list