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
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 > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
Leon Yee 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? >This has nothing to do with union() and everything to do with get(). So read the help page of the latter. -- O__ ---- Peter Dalgaard ?ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907