Kevin Murphy
2001-Jul-27 21:18 UTC
[R] style question: returning multiple arguments - structure or list
I have a question about what is considered good style in R. I have a function which returns several arguments, say [x,y,z]=f(), where sometimes I only care about the first few (so in Matlab, I can write e.g., [x,y] = f()). In R, there seem to be 2 ways to write this, both unsatisfying (to me): LIST f <- function() { ... list(x=x,y=y,z=z) } res <- f() x <- res$x; y <- res$y; z <- res$z STRUCTURE f <- function() { ... structure(x,y=y,z=z) } x <- f() y <- attr(x,"y"); z <- attr(x, "z") Which is considered better style, or does it just depend on the problem? Kevin -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Ross Ihaka
2001-Jul-27 22:01 UTC
[R] style question: returning multiple arguments - structure or list
Kevin Murphy wrote:> > I have a question about what is considered good style in R. > > I have a function which returns several arguments, say [x,y,z]=f(), > where sometimes I only care about the first few > (so in Matlab, I can write e.g., [x,y] = f()). > > In R, there seem to be 2 ways to write this, both unsatisfying (to me): > > LIST > f <- function() { > ... > list(x=x,y=y,z=z) > } > res <- f() > x <- res$x; y <- res$y; z <- res$z > > STRUCTURE > f <- function() { > ... > structure(x,y=y,z=z) > } > x <- f() > y <- attr(x,"y"); z <- attr(x, "z") > > Which is considered better style, or does it just depend on the > problem?The second of these looks a little odd to me. In the first x, y, and z are treated symmetrically while the second is asymmetric. You might also consider whether you want to unpackage the x, y and z values from the list at all. If you are just using the returned values you may as well refer to them as res$x, res$y and res$z. Ross -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Prof Brian D Ripley
2001-Jul-28 06:59 UTC
[R] style question: returning multiple arguments - structure or list
On Fri, 27 Jul 2001, Kevin Murphy wrote:> I have a question about what is considered good style in R. > > I have a function which returns several arguments, say [x,y,z]=f(), > where sometimes I only care about the first few > (so in Matlab, I can write e.g., [x,y] = f()). > > In R, there seem to be 2 ways to write this, both unsatisfying (to me): > > LIST > f <- function() { > ... > list(x=x,y=y,z=z) > } > res <- f() > x <- res$x; y <- res$y; z <- res$z > > > STRUCTURE > f <- function() { > ... > structure(x,y=y,z=z) > } > x <- f() > y <- attr(x,"y"); z <- attr(x, "z") > > Which is considered better style, or does it just depend on the > problem?The latter. There's a third option, to create a new class and return an object of that class, and write appropriate extractor functions. If you look in the code base you will see many more uses of lists than attributes, and heavy use of classes for returned lists. -- 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 272860 (secr) Oxford OX1 3TG, UK Fax: +44 1865 272595 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Thomas J Vogels
2001-Jul-28 14:36 UTC
[R] style question: returning multiple arguments - structure orlist
Hi, you also have the option of "throwing out" the third result by setting it to NULL (taking your LIST option)?> f <- function() { > ... > list(x=x,y=y,z=z) > } > res <- f() > names(jj)[1] "x" "y" "z"> res$z <- NULL > names(res)[1] "x" "y" and then work with res (res$x and res$y)? Regards, -tom -- Email: vogels at cmu.edu Phone: 412.855.2096 On Fri, 27 Jul 2001, Kevin Murphy wrote:> I have a question about what is considered good style in R. > > I have a function which returns several arguments, say [x,y,z]=f(), > where sometimes I only care about the first few > (so in Matlab, I can write e.g., [x,y] = f()). > > In R, there seem to be 2 ways to write this, both unsatisfying (to me): > > LIST > f <- function() { > ... > list(x=x,y=y,z=z) > } > res <- f() > x <- res$x; y <- res$y; z <- res$z > > > STRUCTURE > f <- function() { > ... > structure(x,y=y,z=z) > } > x <- f() > y <- attr(x,"y"); z <- attr(x, "z") > > Which is considered better style, or does it just depend on the > problem?-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._