Hello,
I have a list called ja and I wish to unify list objects if there is some
overlap.
For instance something like
if (length (intersect (ja[[1]], ja[[2]]) !=0) { union (ja[[1]], ja[[2]] }
but of course it should work cumulatively (for larger data sets).
Could you please give me a hint.
Thanks
Hermann
> ja
$A
[1] "A" "B" "F" "G" "H"
$B
[1] "B" "F" "I"
$C
[1] "C" "F" "I" "K"
$D
[1] "D" "L" "M" "N"
$L
[1] "L" "O" "P"
dput (ja)
structure(list(A = c("A", "B", "F", "G",
"H"), B = c("B", "F",
"I"), C = c("C", "F", "I",
"K"), D = c("D", "L", "M",
"N"), L = c("L",
"O", "P")), .Names = c("A", "B",
"C", "D", "L"))
	[[alternative HTML version deleted]]
I guess I found a solution though it is not very elegant.
Hermann
snp.block <- function (huz)
    {
        for (i in names (huz))
             {
                 chain1 <- huz[[i]]
                 for (k in names (huz))
                     {
                         chain2 <- huz[[k]]
                         if (k==i)
                             {
                                 next
                             }
                         else if (length (intersect (chain1, chain2))!=0)
                             {
                                 chain3 <- union (chain1, chain2)
                                 chain1 <- chain3
                                 huz[[i]] <- sort (chain3)
                             }
                         else if (length (intersect (chain1, chain2)) == 0)
                             {
                                 chain3 <- chain1
                                 huz[[i]] <- sort (chain3)
                                 next
                             }
                     }
             }
        huz <- unique (huz)
        return (huz)
    }
2013/11/9 Hermann Norpois <hnorpois@gmail.com>
> Hello,
>
> I have a list called ja and I wish to unify list objects if there is some
> overlap.
> For instance something like
>
> if (length (intersect (ja[[1]], ja[[2]]) !=0) { union (ja[[1]], ja[[2]] }
>
> but of course it should work cumulatively (for larger data sets).
>
> Could you please give me a hint.
>
> Thanks
> Hermann
>
> > ja
> $A
> [1] "A" "B" "F" "G" "H"
>
> $B
> [1] "B" "F" "I"
>
> $C
> [1] "C" "F" "I" "K"
>
> $D
> [1] "D" "L" "M" "N"
>
> $L
> [1] "L" "O" "P"
>
>
> dput (ja)
> structure(list(A = c("A", "B", "F",
"G", "H"), B = c("B", "F",
> "I"), C = c("C", "F", "I",
"K"), D = c("D", "L", "M",
"N"), L = c("L",
> "O", "P")), .Names = c("A", "B",
"C", "D", "L"))
>
	[[alternative HTML version deleted]]
Your specification is a unclear (to me anyway): What do you want to return if the intersection is empty? What if intersect(ja[[i]], ja[[i+1]]) is empty for all i? What if length(intersect( ja[[i]], ja[[i+1]] )) ==0 but intersect(ja[[i]],ja[[i+2]]) is nonempty? Your example isn't -- you did not specify what the return should be on your list. Note also that your example test is wrong: the length of the intersection must =0 not the intersection. -- Bert On Sat, Nov 9, 2013 at 12:45 PM, Hermann Norpois <hnorpois at gmail.com> wrote:> Hello, > > I have a list called ja and I wish to unify list objects if there is some > overlap. > For instance something like > > if (length (intersect (ja[[1]], ja[[2]]) !=0) { union (ja[[1]], ja[[2]] } > > but of course it should work cumulatively (for larger data sets). > > Could you please give me a hint. > > Thanks > Hermann > >> ja > $A > [1] "A" "B" "F" "G" "H" > > $B > [1] "B" "F" "I" > > $C > [1] "C" "F" "I" "K" > > $D > [1] "D" "L" "M" "N" > > $L > [1] "L" "O" "P" > > > dput (ja) > structure(list(A = c("A", "B", "F", "G", "H"), B = c("B", "F", > "I"), C = c("C", "F", "I", "K"), D = c("D", "L", "M", "N"), L = c("L", > "O", "P")), .Names = c("A", "B", "C", "D", "L")) > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.-- Bert Gunter Genentech Nonclinical Biostatistics (650) 467-7374