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

Gabor Grothendieck ggrothendieck at gmail.com
Mon Oct 4 16:07:06 CEST 2010


On Mon, Oct 4, 2010 at 7:51 AM, Berwin A Turlach
<berwin at maths.uwa.edu.au> wrote:
> On Mon, 4 Oct 2010 19:45:23 +0800
> Berwin A Turlach <berwin at maths.uwa.edu.au> wrote:
>
> Mmh,
>
>> R> my.return <- function (vector.of.variable.names) {
>>   sapply(vector.of.variable.names, function(x) list(get(x)))
>> }
>
> make that:
>
> R> my.return <- function (vector.of.variable.names) {
>     sapply(vector.of.variable.names, function(x) get(x))
>   }
>

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)

-- 
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