[R] Union of list elements

Patrick Burns pburns at pburns.seanet.com
Fri Dec 17 20:14:46 CET 2004


Andy, I don't think that you should be so quick to put yourself down.
Your solution just needs a 'sort' put in it so that order doesn't matter.

With Andy's solution the object can now be a character vector rather
than a list and 'match' can be used for testing new items:

newitems <- unique(newitems)
test.result <- match(newitems, values, nomatch=NA)
values <- c(values, newitems[is.na(test.result)])

I think both solutions have their place, and the usage will determine
which is more efficient (where efficiency includes how well it fits in
as well as time and memory use).

Patrick Burns

Burns Statistics
patrick at burns-stat.com
+44 (0)20 8525 0696
http://www.burns-stat.com
(home of S Poetry and "A Guide for the Unwilling S User")

Liaw, Andy wrote:

>>From: Gabor Grothendieck
>>
>>Pat Meyer <paterijk <at> hotmail.com> writes:
>>
>>: 
>>: Hi,
>>: 
>>: First of all, let me thank you all for replying so rapidly 
>>to my first 
>>: question on this list. It was very very helpfull... and I'm 
>>learning R 
>>: faster and faster.
>>: 
>>: I just encountered a second problem, which may also have a 
>>simple solution.
>>: 
>>: Here it is:
>>: 
>>: In my program, a vector is a set of objects.
>>: 
>>: I was looking for a way to store these sets in a big 
>>object. I chose to 
>>: store them in a list.
>>: 
>>: So now I have a list of vectors which looks as follows:
>>: 
>>: [[1]]
>>: [1] "a1" "a3" "a4"
>>: 
>>: [[2]]
>>: [1] "a1" "a4" "a5"
>>: 
>>: [[3]]
>>: [1] "a1" "a5" "a6"
>>: 
>>: Then comes a crucial step where I may have to add the 
>>vector ("a3", "a1", 
>>: "a4") in this list.
>>: 
>>: But as you can see, this set is already present (at 
>>position 1 of my list). 
>>: So it should not be added. If I do a systematic 
>>concatenation, at the end, I 
>>: have a list with too many vectors (where some elements of 
>>my list represent 
>>: the same set).
>>: 
>>: So what I would like to do is a type of Union, but I can't 
>>find a way to do 
>>: it. Unions work only on vectors, and not a vector and a 
>>list of vectors.
>>
>>L <- list(c("a1","a3","a4"), c("a1","a4","a5"), c("a1","a5","a6"))
>>newentry <- c("a3", "a1", "a4")
>>if (!any(sapply(L, setequal, newentry))) L <- c(L, list(newentry))
>>    
>>
>
>Cool, Gabor!  This works even if the sets have the same elements in
>different orders.  Much better than what I had.
>
>Best,
>Andy
>
>
>
>  
>
>>______________________________________________
>>R-help at stat.math.ethz.ch mailing list
>>https://stat.ethz.ch/mailman/listinfo/r-help
>>PLEASE do read the posting guide! 
>>http://www.R-project.org/posting-guide.html
>>
>>
>>    
>>
>
>______________________________________________
>R-help at stat.math.ethz.ch mailing list
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>
>
>
>  
>




More information about the R-help mailing list