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

Hans Ekbrand hans.ekbrand at sociology.gu.se
Thu Sep 30 13:49:32 CEST 2010


If I combine elements into a list

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

the names of the elements seems to get lost in the process:

> str(my.list)
List of 2
 $ : num [1:5] 22.4 12.2 10.9 8.5 9.2
 $ : int [1:5] 11 8 6 9 20

If I explicitly name the elements at list-creation, I get what I want:

my.list <- list(b=b, my.c=my.c)

> str(my.list)
List of 2
 $ b   : num [1:5] 22.4 12.2 10.9 8.5 9.2
 $ my.c: int [1:5] 11 8 6 9 20


Now, is there a way to get list() (or some other function) to
automatically name the elements?

I often use list() in return(), and I am getting tired of having to
repeat myself.

-- 
Hans Ekbrand (http://sociologi.cjb.net) <hans at sociologi.cjb.net>
A. Because it breaks the logical sequence of discussion
Q. Why is top posting bad?
-------------- 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/20100930/4b6d13f3/attachment.bin>


More information about the R-help mailing list