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

Christophe Pallier christophe at pallier.org
Mon Oct 4 16:10:46 CEST 2010


See llist from Hmisc package:

library(Hmisc)
a=rnorm(10)
b=rnorm(5)
llist(a,b)



On Thu, Sep 30, 2010 at 1:49 PM, Hans Ekbrand
<hans.ekbrand at sociology.gu.se> wrote:
> 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?
>
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.9 (GNU/Linux)
>
> iEYEARECAAYFAkykeUwACgkQfCyHKnBQYU4J+ACgrdjMSoyr/Uzt9fpTsietde3n
> d8UAnRskbOM7mDhDexiS7T9LkhRs287P
> =MsDG
> -----END PGP SIGNATURE-----
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>



-- 
Christophe Pallier  <christophe at pallier.org>
tel: +33 (0)1 69 08 79 34
Unité de Neuroimagerie Cognitive INSERM-CEA,
Neurospin center, F91191 Gif-sur-Yvette, France
web site: http://www.unicog.org
personal web site: www.pallier.org



More information about the R-help mailing list