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

Hans Ekbrand hans.ekbrand at sociology.gu.se
Mon Oct 4 15:00:44 CEST 2010


On Mon, Oct 04, 2010 at 07:45:23PM +0800, Berwin A Turlach wrote:
> G'day Hans,
> 
> On Mon, 4 Oct 2010 11:28:15 +0200
> Hans Ekbrand <hans at sociologi.cjb.net> wrote:
> 
> > 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]
> > }
> 
> Well, if you are willing to create a vector with the variable names,
> then simpler solutions should be possible, i.e. solutions that only
> operate on the objects of interest and not on all objects in the global
> environment (which could be a lot depending on your style).  

Actually, what made me want this list-like function was when coding
the return() of the interesting results from a calculation function to
what I imagine is the "global environment" (I have only a vague
concept of that though). So, in the global environment there are very
few objects, while there are more objects in the function where this
list-like function will be used.

Your solution does look way cleaner the falling back to hidden stuff
as .GlobalEnv, so I will definately use it.

In addition, the returned list has is not of a strange class as in
Henriques example.

Thanks,

-- 
Hans Ekbrand (http://sociologi.cjb.net) <hans at sociologi.cjb.net>
GPG Fingerprint: 1408 C8D5 1E7D 4C9C C27E 014F 7C2C 872A 7050 614E
-------------- 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/aeaccd50/attachment.bin>


More information about the R-help mailing list