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

Gabor Grothendieck ggrothendieck at gmail.com
Tue Oct 5 02:33:36 CEST 2010


On Mon, Oct 4, 2010 at 8:27 PM, Hans Ekbrand <hans at sociologi.cjb.net> wrote:
> On Mon, Oct 04, 2010 at 10:07:06AM -0400, Gabor Grothendieck wrote:
>> Some small tweaks.   If you use simplify=FALSE then it will guarantee
>> that a list is returned:
>>
>>    sapply(my.names, get, simplify = FALSE)
>>
>> for example, compare the outputs of:
>>
>>    sapply(c("letters", "LETTERS"), get)
>>    sapply(c("letters", "LETTERS"), get, simplify = FALSE)
>
> Thanks Gabor,
>
> But get() fails to find my objects, though get() successfully finds
> "letters" and "LETTERS" (but they are part of the global environment,
> I assume).

Specify the environment from which you want get to fetch them,  See ?get
For example, below we tell it to look in the current environment:

my.test.function <-  function() {
  a <- 1:3
  b <- c("x", "y")
  sapply(c("a", "b"), get, environment(), simplify = FALSE)
}
my.test.function()


-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com



More information about the R-help mailing list