[R] how to make list() return a list of *named* elements

Hans Ekbrand hans at sociologi.cjb.net
Mon Oct 4 11:28:15 CEST 2010


On Thu, Sep 30, 2010 at 09:34:26AM -0300, Henrique Dallazuanna wrote:
> You should try:
> 
> eapply(.GlobalEnv, I)[c('b', 'my.c')]

Great!

b <- c(22.4, 12.2, 10.9, 8.5, 9.2)
my.c <- sample.int(round(2*mean(b)), 4)

my.return <- function (vector.of.variable.names) {
  eapply(.GlobalEnv, I)[vector.of.variable.names]
}

str(my.return(c("b","my.c")))
List of 2
 $ b   :Class 'AsIs'  num [1:5] 22.4 12.2 10.9 8.5 9.2
 $ my.c:Class 'AsIs'  int [1:4] 18 22 12 3

much nicer than list(b=b, my.c=my.c), especially in real cases with
longer variable names and a lot of variables to return.

Thanks Henrique!

-- 
Hans Ekbrand (http://sociologi.cjb.net) <hans at sociologi.cjb.net>
Q. What is that strange attachment in this mail?
A. My digital signature, see www.gnupg.org for info on how you could
   use it to ensure that this mail is from me and has not been
   altered on the way to you.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 197 bytes
Desc: Digital signature
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20101004/5045f8b7/attachment.bin>


More information about the R-help mailing list