[R] union of list objects if objects intersect

Bert Gunter gunter.berton at gene.com
Sun Nov 10 16:47:33 CET 2013


I'll leave it to others to muddle through your code. It appears to
refer to an earlier post that you did not include.  Note also that you
still have not specified what you want to get from your example ja
list.  Doing so might help you get a useful response, which mine
clearly is not.

Cheers,
Bert

On Sun, Nov 10, 2013 at 7:38 AM, Hermann Norpois <hnorpois at gmail.com> wrote:
> My purpose is to create chains of list objects if elements of the object
> overlap. If there is no overlap the object remains as it is. My plan is to
> identify snps that are in ld. The letters in each list object represent snps
> that are in ld in respect to the first snp (or letter). The first snp (or
> letter) is identical with names (list), respectively.
> Yes, (@Bert) my example was wrong. But the function I posted is correct, I
> hope. At least it does what I want for some sets I tested.
>
> 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/10 Bert Gunter <gunter.berton at gene.com>
>>
>> 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
>
>



-- 

Bert Gunter
Genentech Nonclinical Biostatistics

(650) 467-7374



More information about the R-help mailing list