[R] list concatenation

Charles C. Berry cberry at tajo.ucsd.edu
Tue Jan 11 17:58:36 CET 2011


On Tue, 11 Jan 2011, Georg Otto wrote:

> Dear R gurus,
>
>
> first let me apologize for a question that might hve been answered
> before. I was not able to find the solution yet. I want to concatenate
> two lists of lists at their lowest level.
>
> Suppose I have two lists of lists:
>
> list.1 <- list("I"=list("A"=c("a", "b", "c"), "B"=c("d", "e", "f")),
>
>               "II"=list("A"=c("g", "h", "i"), "B"=c("j", "k", "l")))
>
>
> list.2 <- list("I"=list("A"=c("A", "B", "C"), "B"=c("D", "E", "F")),
>
>               "II"=list("A"=c("G", "H", "I"), "B"=c("J", "K", "L")))
>
>
>
>> list.1
> $I
> $I$A
> [1] "a" "b" "c"
>
> $I$B
> [1] "d" "e" "f"
>
>
> $II
> $II$A
> [1] "g" "h" "i"
>
> $II$B
> [1] "j" "k" "l"
>
>
>> list.2
> $I
> $I$A
> [1] "A" "B" "C"
>
> $I$B
> [1] "D" "E" "F"
>
>
> $II
> $II$A
> [1] "G" "H" "I"
>
> $II$B
> [1] "J" "K" "L"
>
>
> Now I want to concatenate list elements of the lowest levels, so the
> result looks like this:
>
>
> $I
> $I$A
> [1] "a" "b" "c" "A" "B" "C"
>
> $I$B
> [1] "d" "e" "f" "D" "E" "F"
>
>
> $II
> $II$A
> [1] "g" "h" "i" "G" "H" "I"
>
> $II$B
> [1] "j" "k" "l" "J" "K" "L"
>
>
> Has anybody a good solution for that?
>

> mapply( function(x,y) mapply(c, x, y, SIMPLIFY=FALSE),
+         list.1, list.2, SIMPLIFY=FALSE )
$I
$I$A
[1] "a" "b" "c" "A" "B" "C"

$I$B
[1] "d" "e" "f" "D" "E" "F"


$II
$II$A
[1] "g" "h" "i" "G" "H" "I"

$II$B
[1] "j" "k" "l" "J" "K" "L"

HTH,

Chuck


>


> Best,
>
> Georg
>
> ______________________________________________
> 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.
>

Charles C. Berry                            Dept of Family/Preventive Medicine
cberry at tajo.ucsd.edu			    UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/  La Jolla, San Diego 92093-0901



More information about the R-help mailing list