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? Best, Georg ______________________________________________ [hidden email] 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. |
Hi Georg,
This was an interesting challenge to me. Here's what I came up with. The first option meets your desired result, but could get messy with deeper nesting. The second is less code, but is not quite what you want and requires as.data.frame() to give a reasonable result for each list. Calling either option a "good solution" would be rather generous. I'm iinterested to see what other people do. Josh ## Data 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"))) ## Try 1 list.t1 <- list.1 for(i in length(list.1)) { for(j in length(list.1[[i]])) { list.t1[[c(i, j)]] <- c(list.1[[c(i, j)]], list.2[[c(i, j)]]) } } ## Try 2 list.t2 <- as.list(do.call("rbind", lapply(list(list.1, list.2), as.data.frame, stringsAsFactors = FALSE))) ## Results list.t1 list.t2 On Tue, Jan 11, 2011 at 7:44 AM, Georg Otto <[hidden email]> 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? > > Best, > > Georg > > ______________________________________________ > [hidden email] 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. > -- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.com/ ______________________________________________ [hidden email] 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. |
In reply to this post by Georg Otto-2
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 > > ______________________________________________ > [hidden email] 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 [hidden email] UC San Diego http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901 ______________________________________________ [hidden email] 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. |
In reply to this post by Georg Otto-2
Without any error checking, I'd try this:
recurse <- function(l1,l2){ if(is(l1[[1]],"list")) mapply(recurse,l1,l2,SIMPLIFY=F) else {mapply(c,l1,l2,SIMPLIFY=F)} } recurse(list.1,list.2) which recursively traverses each tree (list) in tandem until one is not composed of lists and then concatenates. If the structure of the lists does not agree, this will fail. Regards, David Katz david@davidkatzconsulting.com |
In reply to this post by Joshua Wiley-2
Lists are (isomorphic to) trees with (possibly) labelled nodes. A
completely general solution in which two trees have possibly different topologies and different labels would therefore involve identifying the paths to leaves on each tree, e.g. via depth first search using recursion, and unioning leaves with the same paths (which could be quickly found in R via match() on the paths). This is a standard exercise in a data structures course. Considerable simplification could be effected if tree topologies and/or labels are identical or have other restrictions on them. However, you have not made it clear in your post whether this is the case (it is in your example). -- Bert On Tue, Jan 11, 2011 at 8:04 AM, Joshua Wiley <[hidden email]> wrote: > Hi Georg, > > This was an interesting challenge to me. Here's what I came up with. > The first option meets your desired result, but could get messy with > deeper nesting. The second is less code, but is not quite what you > want and requires as.data.frame() to give a reasonable result for each > list. Calling either option a "good solution" would be rather > generous. I'm iinterested to see what other people do. > > Josh > > ## Data > 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"))) > > ## Try 1 > list.t1 <- list.1 > > for(i in length(list.1)) { > for(j in length(list.1[[i]])) { > list.t1[[c(i, j)]] <- c(list.1[[c(i, j)]], list.2[[c(i, j)]]) > } > } > > ## Try 2 > list.t2 <- as.list(do.call("rbind", lapply(list(list.1, list.2), > as.data.frame, stringsAsFactors = FALSE))) > > ## Results > > list.t1 > list.t2 > > > On Tue, Jan 11, 2011 at 7:44 AM, Georg Otto <[hidden email]> 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? >> >> Best, >> >> Georg >> >> ______________________________________________ >> [hidden email] 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. >> > > > > -- > Joshua Wiley > Ph.D. Student, Health Psychology > University of California, Los Angeles > http://www.joshuawiley.com/ > > ______________________________________________ > [hidden email] 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. > -- Bert Gunter Genentech Nonclinical Biostatistics ______________________________________________ [hidden email] 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. |
Bert Gunter <[hidden email]> writes:
> Lists are (isomorphic to) trees with (possibly) labelled nodes. A > completely general solution in which two trees have possibly different > topologies and different labels would therefore involve identifying > the paths to leaves on each tree, e.g. via depth first search using > recursion, and unioning leaves with the same paths (which could be > quickly found in R via match() on the paths). This is a standard > exercise in a data structures course. > > Considerable simplification could be effected if tree topologies > and/or labels are identical or have other restrictions on them. > However, you have not made it clear in your post whether this is the > case (it is in your example). > Thanks so much to all of you for your very helpful suggestions, that helped me solve my problem. The tree topologies are indeed identical, so the suggested solutions did work, but just for me to learn: Can somebody point me to how a general solution mentioned by Bert would look like? Cheers, Georg ______________________________________________ [hidden email] 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. |
Powered by Nabble | Edit this page |