Discussion:
[R] rbind in array of lists
Cesar Caballero
2014-10-12 06:14:09 UTC
Permalink
Hi all,

I have an array of lists. All lists have the same names and the vectors inside each name have the same dimension.

For instance,

a[1:4]
[[1]]
[[1]]$var1
[1] 1 2 3 4 5

[[1]]$var2
[1] 6 7


[[2]]
[[2]]$var1
[1] 2 4 6 8 10

[[2]]$var2
[1] 12 14

[[3]]
[[3]]$var1
[1] 3 6 9 12 15

[[3]]$var2
[1] 18 21


I would like to apply rbind to concatenate all vectors for a given name, e.g. concatenate all the a[[]]$a
How can I do that?

Thanks very much in advance.

Best wishes
Cesar


----------------------------------------------------------------------
Cesar Caballero
MRI engineer
www.bcbl.eu

Legal disclaimer/Aviso legal/Lege-oharra: www.bcbl.eu/legal-disclaimer
Rui Barradas
2014-10-12 15:45:26 UTC
Permalink
Hello,

Try the following.

do.call(rbind, lapply(a, '[[', "var1"))
do.call(rbind, lapply(a, '[[', "var2"))


Hope this helps,

Rui Barradas
Post by Cesar Caballero
Hi all,
I have an array of lists. All lists have the same names and the vectors inside each name have the same dimension.
For instance,
a[1:4]
[[1]]
[[1]]$var1
[1] 1 2 3 4 5
[[1]]$var2
[1] 6 7
[[2]]
[[2]]$var1
[1] 2 4 6 8 10
[[2]]$var2
[1] 12 14
[[3]]
[[3]]$var1
[1] 3 6 9 12 15
[[3]]$var2
[1] 18 21
I would like to apply rbind to concatenate all vectors for a given name, e.g. concatenate all the a[[]]$a
How can I do that?
Thanks very much in advance.
Best wishes
Cesar
----------------------------------------------------------------------
Cesar Caballero
MRI engineer
www.bcbl.eu
Legal disclaimer/Aviso legal/Lege-oharra: www.bcbl.eu/legal-disclaimer
______________________________________________
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.
David Winsemius
2014-10-12 17:26:37 UTC
Permalink
Post by Rui Barradas
Hello,
Try the following.
do.call(rbind, lapply(a, '[[', "var1"))
do.call(rbind, lapply(a, '[[', "var2"))
Could perhaps (untested) make it more general with:

do.call(rbind, lapply(a, '[[', names(a[[1]])[1]))
do.call(rbind, lapply(a, '[[', names(a[[1]])[2]))

And then seeing that, could perhaps make it a one-liner with:

lapply( names(a[[1]]), function(nm) do.call(rbind, lapply(a, '[[', nm)) )
--
David.
Post by Rui Barradas
Hope this helps,
Rui Barradas
Post by Cesar Caballero
Hi all,
I have an array of lists. All lists have the same names and the vectors inside each name have the same dimension.
For instance,
a[1:4]
[[1]]
[[1]]$var1
[1] 1 2 3 4 5
[[1]]$var2
[1] 6 7
[[2]]
[[2]]$var1
[1] 2 4 6 8 10
[[2]]$var2
[1] 12 14
[[3]]
[[3]]$var1
[1] 3 6 9 12 15
[[3]]$var2
[1] 18 21
I would like to apply rbind to concatenate all vectors for a given name, e.g. concatenate all the a[[]]$a
How can I do that?
Thanks very much in advance.
Best wishes
Cesar
----------------------------------------------------------------------
Cesar Caballero
MRI engineer
www.bcbl.eu
Legal disclaimer/Aviso legal/Lege-oharra: www.bcbl.eu/legal-disclaimer
______________________________________________
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.
______________________________________________
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.
David Winsemius
Alameda, CA, USA
Rui Barradas
2014-10-12 17:31:41 UTC
Permalink
Hello,
Post by David Winsemius
Post by Rui Barradas
Hello,
Try the following.
do.call(rbind, lapply(a, '[[', "var1"))
do.call(rbind, lapply(a, '[[', "var2"))
do.call(rbind, lapply(a, '[[', names(a[[1]])[1]))
do.call(rbind, lapply(a, '[[', names(a[[1]])[2]))
lapply( names(a[[1]]), function(nm) do.call(rbind, lapply(a, '[[', nm)) )
Yes, I had thought of

lapply(seq_along(a[[1]]), function(i) do.call(rbind, lapply(a, '[[', i)))


Rui Barradas
Loading...