Hans Ekbrand
2010-Sep-30 11:49 UTC
[R] how to make list() return a list of *named* elements
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>
Henrique Dallazuanna
2010-Sep-30 12:34 UTC
[R] how to make list() return a list of *named* elements
You should try: eapply(.GlobalEnv, I)[c('b', 'my.c')] On Thu, Sep 30, 2010 at 8:49 AM, Hans Ekbrand <hans.ekbrand@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@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@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. > >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
Gabor Grothendieck
2010-Sep-30 13:10 UTC
[R] how to make list() return a list of *named* elements
On Thu, Sep 30, 2010 at 7:49 AM, 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.A data frame is a list in which every component (i.e. every column) must have the same length (i.e. the same number of rows). data.frame() does preserve names:> data.frame(b, my.c)b my.c 1 22.4 8 2 12.2 9 3 10.9 15 4 8.5 1 5 9.2 14 -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com
Hans Ekbrand
2010-Oct-04 09:31 UTC
[R] how to make list() return a list of *named* elements
On Thu, Sep 30, 2010 at 09:10:16AM -0400, Gabor Grothendieck wrote:> A data frame is a list in which every component (i.e. every column) > must have the same length (i.e. the same number of rows). > data.frame() does preserve names: > > > data.frame(b, my.c) > b my.c > 1 22.4 8 > 2 12.2 9 > 3 10.9 15 > 4 8.5 1 > 5 9.2 14Thanks for your suggestion. However, the reason I used list() was that the different vectors to return usually have different lengths. Admittedly, I should have used another example that explicated this. -- Hans Ekbrand (http://sociologi.cjb.net) <hans at sociologi.cjb.net> GnuPG key: 1024D/7050614E Fingerprint: 1408 C8D5 1E7D 4C9C C27E 014F 7C2C 872A 7050 614E Learn about secure email at http://www.gnupg.org -------------- 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/86a73829/attachment.bin>
Hans Ekbrand
2010-Oct-04 13:01 UTC
[R] how to make list() return a list of *named* elements
On Mon, Oct 04, 2010 at 07:51:10PM +0800, Berwin A Turlach wrote:> R> my.return <- function (vector.of.variable.names) { > sapply(vector.of.variable.names, function(x) get(x)) > }Even better :-) -- 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/01b4fe39/attachment.bin>
Gabor Grothendieck
2010-Oct-04 14:07 UTC
[R] how to make list() return a list of *named* elements
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
Christophe Pallier
2010-Oct-04 14:10 UTC
[R] how to make list() return a list of *named* elements
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
Hans Ekbrand
2010-Oct-05 00:39 UTC
[R] how to make list() return a list of *named* elements
On Mon, Oct 04, 2010 at 04:10:46PM +0200, Christophe Pallier wrote:> See llist from Hmisc package: > > library(Hmisc) > a=rnorm(10) > b=rnorm(5) > llist(a,b)Ah, that seems like what I want! My old, ugly and redunant code looked like this list(utdrag=utdrag, n.fires.at.sites.with.one.or.more.fires=n.fires.at.sites.with.one.or.more.fires, more.than.one.fire.in.these.clusters=more.than.one.fire.in.these.clusters, n.fires.in.clusters.with.more.than.one.fire=n.fires.in.clusters.with.more.than.one.fire, n.fires.in.top.ten.clusters=n.fires.in.top.ten.clusters, m=m, ?versta.fem.procenten=?versta.fem.procenten, these.cluster.have.alot.of.members=these.cluster.have.alot.of.members, n.fires.in.hotspots=n.fires.in.hotspots, prop.concentrated.fires=prop.concentrated.fires, d=d, real.vector=real.vector, the.real=the.real, censored.real=censored.real, half.of.effect.at=half.of.effect.at) The new, nice-looking code looks like this: llist(utdrag, n.fires.at.sites.with.one.or.more.fires, more.than.one.fire.in.these.clusters, n.fires.in.clusters.with.more.than.one.fire, n.fires.in.top.ten.clusters, m, ?versta.fem.procenten, these.cluster.have.alot.of.members, n.fires.in.hotspots, prop.concentrated.fires, d, real.vector, the.real, censored.real, half.of.effect.at) Thank you all r-helpers! -------------- 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/20101005/e622eb9d/attachment.bin>