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 ? Then shoud l use g[1] or g[[1]] ? Thank you for you help. Randall [[alternative HTML version deleted]]
On Wed, 17 Feb 2010, Randall Wrong wrote:> Dear R users, > > I have multi-argument returns in a function and I am warned by the program > they are deprecated.Defunct as from the next R release.> I have found this in the R-help archives :in 2001!> 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 :return(list(x=x,y=y,z=z)) is the preferred replacement. (As the help page for return() has long said.)> g=function() { > > #... > > result=list(x,y,z) > return(result) > } > > Is that correct ? > > Then shoud l use g[1] or g[[1]] ?No change is needed (I think you mean g()$x etc) as return(x,y,z) and return(list(x=x,y=y,z=z)) are identical in their effects.> Thank you for you help. > > Randall-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
Hi, 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: myvalue$x Hope that helps, -steve -- 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