# [R] A question about "union"

Wacek Kusnierczyk Waclaw.Marcin.Kusnierczyk at idi.ntnu.no
Fri Oct 17 13:11:49 CEST 2008

```... but it seems to me a better solution to have the vectors you need in
a list, and then iterate over the list, rather than have to look the
vectors up by name:

vectors = list(v1, v2, 1:10, ...)
u = NULL
for (v in vectors) u = union(u, v)

when you use get, you start to explicitly play with environments, and
you should be knowing what you do, otherwise ugly surprizes await.

vQ

Leon Yee wrote:
> Cool! It works!
>
> Thank you very much!
>
> Leon
>
> Yihui Xie wrote:
>> You need to get() their elements instead of merely using variable names.
>>
>> Regards,
>> Yihui
>> --
>> Yihui Xie <xieyihui at gmail.com>
>> Phone: +86-(0)10-82509086 Fax: +86-(0)10-82509086
>> Mobile: +86-15810805877
>> Homepage: http://www.yihui.name
>> School of Statistics, Room 1037, Mingde Main Building,
>> Renmin University of China, Beijing, 100872, China
>>
>>
>>
>> On Fri, Oct 17, 2008 at 6:23 PM, Leon Yee <yee.leon at gmail.com> wrote:
>>> Dear all,
>>>
>>>   I have a question about "union".  "union" handles two vectors'
>>> elements to
>>> get their union, but I have many vectors and I want to get the union
>>> of all
>>> of them. So I wrote a loop:
>>>
>>> all <-c();
>>> for(var in ls(pattern="xyz"))
>>> # all of the vectors with pattern of "xyz"
>>> {
>>>   all <- union(all, var);
>>> }
>>>
>>>
>>> but the result is like this: ("xyzblabla1", "xyzblabla2", "abcxyz",
>>> ... ) --
>>> just a collection of the variable names, not the union of their
>>> elements.
>>>
>>> How can I solve this problem?
>>>
>>> thanks!
>>>
>>> Leon
>>>
>>> ______________________________________________
>>> R-help at r-project.org mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

--
-------------------------------------------------------------------------------
Wacek Kusnierczyk, MD PhD

Email: waku at idi.ntnu.no
Phone: +47 73591875, +47 72574609

Department of Computer and Information Science (IDI)
Faculty of Information Technology, Mathematics and Electrical Engineering (IME)
Norwegian University of Science and Technology (NTNU)
Sem Saelands vei 7, 7491 Trondheim, Norway
Room itv303

Bioinformatics & Gene Regulation Group
Department of Cancer Research and Molecular Medicine (IKM)
Faculty of Medicine (DMF)
Norwegian University of Science and Technology (NTNU)
Laboratory Center, Erling Skjalgsons gt. 1, 7030 Trondheim, Norway
Room 231.05.060

```